Question

I understand blocking code is a sin when it comes to event loops (i.e. NodeJS), but what about with greenlets (which I believe are green threads)? Is there an issue running code that calls blocking functions?

Was it helpful?

Solution

Greenlets themselves without an event loop as provided by gevent are very primitive. A blocking call from within one greenlet will block all greenlets since greenlets alone have no capability to yield on IO operations, and no scheduler. Gevent's monkey-patching of socket and file IO is generally enough to enable non-blocking IO even with databases provided that the database library is written in python and uses sockets. Or alternately you can patch the library yourself.

OTHER TIPS

Calling function that blocks event loop is of course issue, because other green threads will be waiting for end of this function.
But if you use Gevent you can call blocking functions. Gevent patches common python blocking functions. Write this at the start of program and Gevent will patch all blocking functions:

from gevent import monkey
monkey.patch_all()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top