gevent.hub

class gevent.hub.Hub(loop=None, default=None)

A greenlet that runs the event loop.

It is created automatically by get_hub().

NOT_ERROR = (<class 'greenlet.GreenletExit'>, <type 'exceptions.SystemExit'>)
SYSTEM_ERROR = (<type 'exceptions.KeyboardInterrupt'>, <type 'exceptions.SystemExit'>, <type 'exceptions.SystemError'>)
backend = None
format_context = 'pprint.pformat'
loop_class = ['gevent.core.loop']
resolver
resolver_class = ['gevent.resolver_thread.Resolver', 'gevent.resolver_ares.Resolver', 'gevent.socket.BlockingResolver']
threadpool
threadpool_class = ['gevent.threadpool.ThreadPool']
threadpool_size = 10
handle_error(context, type, value, tb)
handle_system_error(type, value)
print_exception(context, type, value, tb)
switch()
switch_out()
wait(watcher)
cancel_wait(watcher, error)
run()
join(timeout=None, event=None)

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

If timeout is provided, wait no longer for the specified number of seconds. If event was provided, exit when it was signalled with Event.set() method.

Returns True if exited because the loop finished execution. Returns False if exited because of timeout expired or event was signalled.

destroy(destroy_loop=None)
gevent.hub.get_hub(*args, **kwargs)

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(hub=None)

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()
>>> timer = get_hub().loop.timer(0.1)
>>> timer.start(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()
>>> timer = get_hub().loop.timer(0.1)
>>> timer.start(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.