gevent.hub

class gevent.hub.Hub

A greenlet that runs the event loop.

It is created automatically by get_hub().

switch()
run()
shutdown()
exception gevent.hub.DispatchExit(code)
gevent.hub.get_hub()
class gevent.hub.Waiter

A low level communication utility for greenlets.

Wrapper around greenlet’s switch() and throw() calls that makes them somewhat safer:

  • switching will occur only if the waiting greenlet is executing get() method currently;
  • any error raised in the greenlet is handled inside switch() and throw()
  • if switch()/throw() is called before the receiver calls get(), then Waiter will store the value/exception. The following get() will return the value/raise the exception.

The switch() and throw() methods must only be called from the Hub greenlet. The get() method must be called from a greenlet other than Hub.

>>> result = Waiter()
>>> _ = core.timer(0.1, result.switch, 'hello from Waiter')
>>> result.get() # blocks for 0.1 seconds
'hello from Waiter'

If switch is called before the greenlet gets a chance to call get() then Waiter stores the value.

>>> result = Waiter()
>>> _ = core.timer(0.1, result.switch, 'hi from Waiter')
>>> sleep(0.2)
>>> result.get() # returns immediatelly without blocking
'hi from Waiter'

Warning

This a limited and dangerous way to communicate between greenlets. It can easily leave a greenlet unscheduled forever if used incorrectly. Consider using safer Event/AsyncResult/Queue classes.