eventlet.event.Event

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调度另外一个greenthread
waited for result

Returns immediately if the event has already occurred.

>>> evt.wait()
'result'
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MediaPlayer.Event 是一个枚举类型,它定义了 MediaPlayer 的事件类型。在 VLC Media Player 中,当媒体播放器的状态发生变化时,会生成不同类型的事件,例如播放、暂停、停止、结束等。通过监听 MediaPlayer.Event,我们可以在特定的事件发生时执行相应的操作。 MediaPlayer.Event 包括以下事件类型: - MediaPlayer.Event.MediaChanged:媒体已更改 - MediaPlayer.Event.Opening:正在打开媒体 - MediaPlayer.Event.Buffering:正在缓冲媒体 - MediaPlayer.Event.Playing:正在播放媒体 - MediaPlayer.Event.Paused:媒体已暂停 - MediaPlayer.Event.Stopped:媒体已停止 - MediaPlayer.Event.EndReached:媒体已结束 - MediaPlayer.Event.EncounteredError:媒体播放出错 - MediaPlayer.Event.TimeChanged:媒体播放时间已更改 - MediaPlayer.Event.PositionChanged:媒体播放位置已更改 我们可以通过设置 MediaPlayer 对应的监听器来监听这些事件,例如: ``` mediaPlayer.addMediaPlayerEventListener(new MediaPlayerEventAdapter() { @Override public void buffering(MediaPlayer mediaPlayer, float newCache) { // 缓存进度发生变化 } @Override public void playing(MediaPlayer mediaPlayer) { // 开始播放 } @Override public void paused(MediaPlayer mediaPlayer) { // 暂停播放 } @Override public void stopped(MediaPlayer mediaPlayer) { // 停止播放 } @Override public void finished(MediaPlayer mediaPlayer) { // 播放完成 } @Override public void error(MediaPlayer mediaPlayer) { // 播放出错 } }); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值