pythongeventgreenlets

Why python gevent.joinall execute all greenlets


I have the following Python code:

>>> import gevent
>>> from gevent import monkey; monkey.patch_all()
>>>
>>> def fooFn(k):
...     return 'gevent_'+k
...
>>> threads = []
>>> threads.append(gevent.spawn(fooFn,'0'))
>>> threads.append(gevent.spawn(fooFn,'1'))
>>>
>>> gevent.joinall([threads[1]])
>>>
>>> print threads[1].value
gevent_1
>>> print threads[0].value
gevent_0
>>>

As seen above, threads[0].value got a proper value from the fooFn. This means that the threads[0] greenlet was executed.

Why did this this happen when I passed only the threads[1] greenlet to gevent.joinall?

How can I make sure that only those greenlets are executed which are actually passed to gevent.joinall?


Solution

  • Your greenlets are scheduled immediately when you call greenlet.spawn(). In other words, they are created and started at once when you call spawn(). That's why the first greenlet is finished running - both greenlets were executing from the moment you spawned them, and by the time you got around to looking up the results from greenlet 1, both were done executing.

    gevent.joinall() doesn't execute greenlets - it only tells the main thread (the one that actually spawned them) to wait for the ones passed in as parameters to finish running. Not calling joinall runs the risk of the main thread finishing and exiting before the results of the greenlets in joinall() has been reached, and then who's going to handle their results?

    Here, you did two things that you should change in order to see gevents behave like you want:

    Using start to indicate the greenlet should start running at this moment is a good idea. So: create two greenlet objects, and only call start on one of them to have only that one execute.