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?

有帮助吗?

解决方案

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.

其他提示

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()
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top