gevent.hub

class gevent.hub.Hub(loop=None)

A greenlet that runs the event loop.

It is created automatically by get_hub().

loop_class

alias of loop

handle_error(where, type, value, tb)
switch()
wait(watcher)
cancel_wait(watcher, error)
run()
join()

Wait for the event loop to finish. Exits only when there are no more spawned greenlets, started servers, active timeouts or watchers.

gevent.hub.get_hub(*args)

Return the hub for the current thread.

If hub does not exists in the current thread, the new one is created with call to get_hub_class().

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()
>>> _ = get_hub().loop.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()
>>> _ = get_hub().loop.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.