2

In my Python program, I use eventlet to create a lot of greenthreads, just want to know if the current greenthread sleeps, which one of the rest will get scheduled and run? Any scheduler in the eventlet?

Thanks!

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
Eric
  • 688
  • 1
  • 6
  • 17

1 Answers1

1

Generally speaking, having two greenthreads ready, which one is going to run is undefined. You should write your programs as if it's random choice each time.

Implementation detail is that we have scheduler queue with FIFO semantics. But it gets a little more complicated waiting for time (>0 seconds) or waiting for file descriptors (including network).

Oh and we have official Github repository here https://github.com/eventlet/eventlet

temoto
  • 5,394
  • 3
  • 34
  • 50
  • If which greenthread is going to run is undefined, then I think it will also have some similar drawback with OS thread, like complicated programming pattern, could cause deadlock, etc. So I'd like to know, compared with OS thread, what benefit I can get from greenthread, only "lightweight" and less overhead to switch? But with OS thread, I can fully utilize the multiple CPU cores in one Python program which can not be achieved with greenthread. – Eric Dec 20 '14 at 02:15
  • And another question is, when I call eventlet.sleep(5) in greenthread A to give up execution to other greenthreads, how can eventlet guarantee greenthread A is scheduled again after 5 seconds? – Eric Dec 20 '14 at 02:25
  • @Eric: "I can fully utilize the multiple CPU cores in one Python program" - [Nope](http://stackoverflow.com/questions/1294382/what-is-a-global-interpreter-lock-gil), sorry. – Kevin Dec 20 '14 at 05:03
  • @Kevin, thanks, those info are very helpful! So a new question comes, in a Python program, why do we need multiple OS threads at all? Since at a given instant, there will only one OS thread can be run due to GIL, I not know why we need OS thread in Python program. I think multiple OS process + multiple greenthread in OS process should be enough, right? – Eric Dec 20 '14 at 10:15
  • @Eric: OS threads are primarily used for multiplexing I/O. They can also be used in conjunction with NumPy and some other libraries which release the GIL during computationally expensive operations. – Kevin Dec 20 '14 at 19:34
  • @Eric on `sleep(5)`. In some sense, eventlet guarantees that calling greenthread will execute in 5 seconds **or more** by scheduling it right now at the `sleep` call. eventlet maintains a set of timers and knows which timer should wake up which greenthread. There is no a guarantee that calling greenthread would resume in **exactly** 5 seconds. It may be 5.00001 or 5.00023 or even 6.14 if some other greenthread have been doing long CPU intensive work and not allowed scheduler to switch. So please don't build nuclear rockets with Eventlet. It's great for business network applications. – temoto Dec 21 '14 at 12:27