One of my most common gevent use cases is to use the Queue class to process items.
An example from gevent-tutorial:
This works great if you can ensure the queue is never empty, but not so great where producers may not always be active. A better way to define your workers would be to take advantage of Python’s generator idiom:
But that leads to this error:
gevent.exceptions.LoopExit: This operation would block forever
Why? simple, once there are no other greenlets for gevent to switch to (no producers producing…) the last greenlet would block the main loop forever.
To ensure a timely exit of your worker either when the producer shuts down or you receive a signal you can use the StopIteration builtin in Python 3 to gracefully stop your worker.
Hopefully this enables you to write clearer and better gevent code.