I am trying to create a simple producer-consumer system in Gevent, but my script does not exit:
import gevent from gevent.queue import * import time import random q = Queue() workers = [] def do_work(wid, value): """ Actual blocking function """ gevent.sleep(random.randint(0,2)) print 'Task', value, 'done', wid return def worker(wid): """ Consumer """ while True: item = q.get() do_work(wid, item) def producer(): """ Producer """ for i in range(4): workers.append(gevent.spawn(worker, random.randint(1, 100000))) for item in range(1, 9): q.put(item) producer() gevent.joinall(workers)
I could not find good examples / tutorials on using Gevent, so what I pasted above is what I exhausted from the Internet.
Several workers are activated, items enter the queue, but even when everything in the queue ends, the main program does not exit. I have to press CTRL ^ C
What am I doing wrong?
Thanks.
On the side of the note: if there is something my script can improve, let me know. Simple things like checking when the queue is empty, etc.
source share