Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between gevent.wait and gevent.joinall?

Tags:

python

gevent

Let's say tasks is a list of Greenlet objects. Now what's the difference between

gevent.wait(tasks)

and

gevent.joinall(tasks)

?

like image 698
bigblind Avatar asked Feb 08 '23 20:02

bigblind


1 Answers

Not much! joinall actually calls wait internally, and is a pretty short function (source code):

def joinall(greenlets, timeout=None, raise_error=False, count=None):
    if not raise_error:
        return wait(greenlets, timeout=timeout, count=count)

    done = []
    for obj in iwait(greenlets, timeout=timeout, count=count):
        if getattr(obj, 'exception', None) is not None:
            if hasattr(obj, '_raise_exception'):
                obj._raise_exception()
            else:
                raise obj.exception
        done.append(obj)
    return done

As you can see, unless you pass raise_error=True, joinall is essentially a passthrough to wait.

If you do pass raise_error=True, then joinall goes through your greenlets, and raises an exception if one of your them raises one (note that it uses iwait instead of wait, so the exception will be raised as soon as one greenlet raises).

like image 51
Thomas Orozco Avatar answered Feb 15 '23 10:02

Thomas Orozco