Search code examples
pythonflaskgevent

python gevent handling long blocking operations


I am using gevent with flask and pymongo. I have an API endpoint where I will need to loop over a large number of results coming from pymongo to do some CPU calculation (speed mean, distances between geocordinates, ...).

I am wondering if gevent provides some functionnality to handle this kind of long blocking operation. It seems I can use gevent.sleep in my loop to trigger context switch between greenlets but it does not seem very clean.


Solution

  • Using gevent.sleep(0) to yield to the hub (what you mean by context switch) is actually perfectly legitimate and the way to go. This is clean and the way mentionned in the doc gevent doc.