what does greenthread.sleep do?

1.2k Views Asked by At

I'm pretty new with eventlet and have some questions on sleep()

I tested with a small piece of code.

At first I spawned 3 greenthreads and then called greenthread.sleep(0), then these 3 greenthreads all came to execute the functions in them. what's going on?

does sleep() mean execute all the greenthread spawned? what does the argument 0 we passed in mean?

Here is the code:

import eventlet
from eventlet import greenthread
from eventlet import event
evt = event.Event()

def func1():
    print "starting func1"
    evt.wait()
    print "stopping func1"

def func2():
    print "starting func2"
    evt.wait()
    print "stopping func2"

def func3():
    evt.send()

gt1 = greenthread.spawn(func1)
gt2 = greenthread.spawn(func2)
gt3 = greenthread.spawn(func3)
greenthread.sleep(0)
1

There are 1 best solutions below

0
On BEST ANSWER

That's a great question, it deserves a special place in Eventlet documentation.

eventlet.sleep(0) reschedules the calling greenthread to the end of run queue. If there were any other greenthreads waiting to run, they will execute now.

Current implementation detail of Eventlet has a certain guarantee that if you call sleep, the calling greenthread will not continue until all other greenthreads that are ready to execute are finished or came to similar wait state. Started as implementation detail, we keep it as a public API now: call sleep(0) to let others run.