eventlet.event.Event

来源:互联网 发布:c语言if语句例题 编辑:程序博客网 时间:2024/06/09 14:13

http://eventlet.net/doc/modules/event.html

功能:用来在协程之间通信

class eventlet.event.Event

An abstraction where an arbitrary number of coroutines(任意数量的协同程序) can wait for one event from another.

Events are similar to a Queue that can only hold one item, but differ in two important ways:

  1. calling send() never unschedules the current greenthread
  2. send() can only be called once; create a new event to send again.

They are good for communicating results between coroutines, and are the basis for how GreenThread.wait() is implemented(他们有助于在协同程序之间传达结果,并且是实现GreenThread.wait()的基础).

>>> from eventlet import event>>> import eventlet>>> evt = event.Event()>>> def baz(b):...     evt.send(b + 1)...>>> _ = eventlet.spawn_n(baz, 3)>>> evt.wait()4
ready()

Return true if the wait() call will return immediately. Used to avoid waiting for things that might take a while to time out. For example, you can put a bunch of events into a list, and then visit them all repeatedly, calling ready() until one returns True, and then you can wait() on that one.

例如,您可以将一堆事件放入列表中,然后重复访问它们,调用ready()直到返回True,然后可以wait()在那一个。

send(result=Noneexc=None)

Makes arrangements for the waiters to be woken with the result and then returns immediately to the parent.

>>> from eventlet import event>>> import eventlet>>> evt = event.Event()>>> def waiter():...     print('about to wait')...     result = evt.wait()...     print('waited for {0}'.format(result))>>> _ = eventlet.spawn(waiter)>>> eventlet.sleep(0)about to wait>>> evt.send('a')>>> eventlet.sleep(0)waited for a

It is an error to call send() multiple times on the same event.

>>> evt.send('whoops')Traceback (most recent call last):...AssertionError: Trying to re-send() an already-triggered event.

Use reset() between send() s to reuse an event object.

send_exception(*args)

Same as send(), but sends an exception to waiters.

The arguments to send_exception are the same as the arguments to raise. If a single exception object(异常对象) is passed in, it will be re-raised when wait() is called, generating a new stacktrace.           send_exception的参数与引用的参数相同。 如果传入一个异常对象,调用wait()时会重新生成一个异常对象,生成一个新的堆栈跟踪

>>> from eventlet import event>>> evt = event.Event()>>> evt.send_exception(RuntimeError())>>> evt.wait()Traceback (most recent call last):  File "<stdin>", line 1, in <module>  File "eventlet/event.py", line 120, in wait    current.throw(*self._exc)RuntimeError

If it’s important to preserve(保护) the entire original stack trace, you must pass in the entire sys.exc_info() tuple.

>>> import sys>>> evt = event.Event()>>> try:...     raise RuntimeError()... except RuntimeError:...     evt.send_exception(*sys.exc_info())...>>> evt.wait()Traceback (most recent call last):  File "<stdin>", line 1, in <module>  File "eventlet/event.py", line 120, in wait    current.throw(*self._exc)  File "<stdin>", line 2, in <module>RuntimeError

Note that doing so stores a traceback object directly on the Event object, which may cause reference cycles. See the sys.exc_info() documentation.

wait()

Wait until another coroutine calls send(). Returns the value the other coroutine passed to send().

>>> from eventlet import event>>> import eventlet>>> evt = event.Event()>>> def wait_on():...    retval = evt.wait()...    print("waited for {0}".format(retval))>>> _ = eventlet.spawn(wait_on) #执行函数wait_on,但此函数有wait,引起调度,切换到main greenlet>>> evt.send('result') >>> eventlet.sleep(0)#让hub调度另外一个greenthreadwaited for result

Returns immediately if the event has already occurred.

>>> evt.wait()'result'
原创粉丝点击