4

Firstly, is it safe to mix green threads such as eventlet or gevent with python native threads from the standard library, i.e. Lib/threading.py in the same python process?

Secondly, if it is safe, is it a bad idea?

David Watson
  • 3,394
  • 2
  • 36
  • 51

2 Answers2

6

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.

David Wolever
  • 148,955
  • 89
  • 346
  • 502
-2

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.

Ivo
  • 5,378
  • 2
  • 18
  • 18
  • I think some explanation of why it's not a good idea is missing here. – Niklas B. Mar 11 '12 at 17:00
  • @NiklasB. Maybe because multithreading is always a possible source for confusion and hard-to-track bugs. So if you don't get anything (or just very little) from multithreading, better avoid it. – sloth Jun 12 '12 at 07:11