gevent.event – Notifications of multiple listeners

class Event

Bases: object

A synchronization primitive that allows one greenlet to wake up one or more others. It has the same interface as threading.Event but works across greenlets.

An event object manages an internal flag that can be set to true with the set() method and reset to false with the clear() method. The wait() method blocks until the flag is true.

is_set()
isSet()
ready()

Return true if and only if the internal flag is true.

clear()

Reset the internal flag to false.

Subsequently, threads calling wait() will block until set() is called to set the internal flag to true again.

isSet()

Return true if and only if the internal flag is true.

is_set()

Return true if and only if the internal flag is true.

Register a callback to call when the internal flag is set to true.

callback will be called in the Hub, so it must not use blocking gevent API. callback will be passed one argument: this instance.

ready()

Return true if and only if the internal flag is true.

set()

Set the internal flag to true.

All greenlets waiting for it to become true are awakened. Greenlets that call wait() once the flag is true will not block at all.

Remove the callback set by rawlink()

wait(timeout=None)

Block until the internal flag is true.

If the internal flag is true on entry, return immediately. Otherwise, block until another thread calls set() to set the flag to true, or until the optional timeout occurs.

When the timeout argument is present and not None, it should be a floating point number specifying a timeout for the operation in seconds (or fractions thereof).

Returns:The value of the internal flag (True or False). (If no timeout was given, the only possible return value is True.)
class AsyncResult

Bases: object

A one-time event that stores a value or an exception.

Like Event it wakes up all the waiters when set() or set_exception() is called. Waiters may receive the passed value or exception by calling get() instead of wait(). An AsyncResult instance cannot be reset.

To pass a value call set(). Calls to get() (those that are currently blocking as well as those made in the future) will return the value:

>>> result = AsyncResult()
>>> result.set(100)
>>> result.get()
100

To pass an exception call set_exception(). This will cause get() to raise that exception:

>>> result = AsyncResult()
>>> result.set_exception(RuntimeError('failure'))
>>> result.get()
Traceback (most recent call last):
 ...
RuntimeError: failure

AsyncResult implements __call__() and thus can be used as link() target:

>>> import gevent
>>> result = AsyncResult()
>>> gevent.spawn(lambda : 1/0).link(result)
>>> try:
...     result.get()
... except ZeroDivisionError:
...     print('ZeroDivisionError')
ZeroDivisionError
exc_info

The three-tuple of exception information if set_exception() was called.

exception

Holds the exception instance passed to set_exception() if set_exception() was called. Otherwise None.

get(block=True, timeout=None)

Return the stored value or raise the exception.

If this instance already holds a value or an exception, return or raise it immediatelly. Otherwise, block until another greenlet calls set() or set_exception() or until the optional timeout occurs.

When the timeout argument is present and not None, it should be a floating point number specifying a timeout for the operation in seconds (or fractions thereof).

Parameters:block (bool) – If set to False and this instance is not ready, immediately raise a Timeout exception.
get_nowait()

Return the value or raise the exception without blocking.

If this object is not yet ready, raise gevent.Timeout immediately.

Register a callback to call when a value or an exception is set.

callback will be called in the Hub, so it must not use blocking gevent API. callback will be passed one argument: this instance.

ready()

Return true if and only if it holds a value or an exception

set(value=None)

Store the value and wake up any waiters.

All greenlets blocking on get() or wait() are awakened. Subsequent calls to wait() and get() will not block at all.

set_exception(exception, exc_info=None)

Store the exception and wake up any waiters.

All greenlets blocking on get() or wait() are awakened. Subsequent calls to wait() and get() will not block at all.

Parameters:exc_info (tuple) – If given, a standard three-tuple of type, value, traceback as returned by sys.exc_info(). This will be used when the exception is re-raised to propagate the correct traceback.
successful()

Return true if and only if it is ready and holds a value

Remove the callback set by rawlink()

value

Holds the value passed to set() if set() was called. Otherwise, None

wait(timeout=None)

Block until the instance is ready.

If this instance already holds a value, it is returned immediately. If this instance already holds an exception, None is returned immediately.

Otherwise, block until another greenlet calls set() or set_exception() (at which point either the value or None will be returned, respectively), or until the optional timeout expires (at which point None will also be returned).

When the timeout argument is present and not None, it should be a floating point number specifying a timeout for the operation in seconds (or fractions thereof).

Note

If a timeout is given and expires, None will be returned (no timeout exception will be raised).

Next page: gevent.queue – Synchronized queues