Answers for "thread and queue python"

2

python get item from queue

"""put and get items from queue"""
>>> from Queue import Queue
>>> q = Queue()
>>> q.put(1)
>>> q.put(2)
>>> q.put(3)
>>> print list(q.queue)
[1, 2, 3]

>>> q.get()
1
>>> print list(q.queue)
[2, 3]
Posted by: Guest on April-04-2020
1

python threading queue

import threading, queue

q = queue.Queue()

def worker():
    while True:
        item = q.get()
        print(f'Working on {item}')
        print(f'Finished {item}')
        q.task_done()

# turn-on the worker thread
threading.Thread(target=worker, daemon=True).start()

# send thirty task requests to the worker
for item in range(30):
    q.put(item)
print('All task requests sentn', end='')

# block until all tasks are done
q.join()
print('All work completed')
Posted by: Guest on October-29-2020

Python Answers by Framework

Browse Popular Code Answers by Language