Search code examples
pythongevent

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


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

gevent.wait(tasks)

and

gevent.joinall(tasks)

?


Solution

  • 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).