What does greenthread.sleep do?

I am new to eventlet and have some questions about sleep ()

I have tested a small piece of code.

I first spawned 3 greenthreads and then called greenthread.sleep (0), after which these 3 greenthreads came to execute functions in them. what's happening?

does sleep () mean executing all spawned greenthread? what does argument 0 mean on average?

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)

      

+3


source to share


1 answer


This is a great question and deserves a special place in the Eventlet documentation.

eventlet.sleep(0)

redirects the caller greenthread to the end of the run queue. If there were other greenthreads to start, they will run now.



The current implementation information Eventlet has some guarantee that when sleep is called, the caller of greenthread will not continue until all other greenthreads ready to run have finished or have entered a similar wait state. Let's start with the implementation details. We now save it as a public API: we call sleep(0)

it for others to execute.

+1


source







All Articles