단일 파이썬 프로세스에서 녹색 스레드와 기본 스레드를 혼합하는 것이 안전합니까?
-
10-12-2019 - |
문제
첫째, 표준 라이브러리에서 Python 네이티브 스레드로 이벤트 또는 GEVENT와 같은 녹색 스레드를 혼합하는 것이 안전합니다. 동일한 파이썬 프로세스의 lib / threading.py?
둘째, 안전하다면 나쁜 생각입니까?
해결책
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.