gevent.event – Notifications of multiple listeners

class gevent.event.Event

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()

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.

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.

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

Return the value of the internal flag (True or False).

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.

Remove the callback set by rawlink()

class gevent.event.AsyncResult

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

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

To pass a value call set(). Calls to get() (those that 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:

>>> result = AsyncResult()
>>> gevent.spawn(lambda : 1/0).link(result)
>>> result.get()
Traceback (most recent call last):
 ...
ZeroDivisionError: integer division or modulo by zero
value

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

exception None

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

ready()

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

successful()

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

set(value=None)

Store the value. Wake up the waiters.

All greenlets blocking on get() or wait() are woken up. Sequential calls to wait() and get() will not block at all.

set_exception(exception)

Store the exception. Wake up the waiters.

All greenlets blocking on get() or wait() are woken up. Sequential calls to wait() and get() will not block at all.

get(block=True, timeout=None)

Return the stored value or raise the exception.

If this instance already holds a value / an exception, return / 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).

get_nowait()

Return the value or raise the exception without blocking.

If nothing is available, raise gevent.Timeout immediatelly.

wait(timeout=None)

Block until the instance is ready.

If this instance already holds a value / an exception, return immediatelly. Otherwise, block until another thread 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).

Return value.

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.

Remove the callback set by rawlink()

Next page: gevent.queue – Synchronized queues