Безопасно ли смешивать зеленые нити и родные потоки в одном процессе Python?

StackOverflow https://stackoverflow.com//questions/9639466

Вопрос

Во-первых, безопасно ли смешивать зеленые нити, такие как Eventlet или Gevent с нативными потоками Python из стандартной библиотеки, I.E. lib / Threading.py в том же процессе Python?

Во-вторых, если это безопасно, это плохое представление?

Это было полезно?

Решение

With gevent < 1.0 it is most likely a bad idea, as libevent isn't thread safe. You might be okay if you make sure that the OS threads never interact with libevent… But that could be tricky, and bugs cause by libevent's lack of thread safety don't sound like any fun to debug.

However, gevent >= 1.0 uses libev, which is entirely thread safe. So (as far as I can tell) there's no problem mixing green threads and OS threads.

Другие советы

gevent provides monkey patching abilities for threads.

Greenlets only switch to eachother within one thread, so one thread's greenlets will be completely separate and incommunicado from another's.

I wouldn't suggest using multiple threads of greenlets until you've shown you have a performance need to do so.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top