- 论坛徽章:
- 1
|
本帖最后由 seufy88 于 2013-12-25 16:06 编辑
最近在看AnyEvent
在讲解到$cv->recv时,有几句话不是很懂。
$cv->recv
Wait (blocking if necessary) until the ->send or ->croak methods have been called on $cv, while servicing other watchers normally.
You can only wait once on a condition - additional calls are valid but will return immediately.
If an error condition has been set by calling ->croak, then this function will call croak.
In list context, all parameters passed to send will be returned, in scalar context only the first one will be returned.
Note that doing a blocking wait in a callback is not supported by any event loop, that is, recursive invocation of a blocking ->recv is not allowed and the recv call will croak if such a condition is detected. This requirement can be dropped by relying on Coro::AnyEvent , which allows you to do a blocking ->recv from any thread that doesn't run the event loop itself. Coro::AnyEvent is loaded automatically when Coro is used with AnyEvent, so code does not need to do anything special to take advantage of that: any code that would normally block your program because it calls recv, be executed in an async thread instead without blocking other threads.
Not all event models support a blocking wait - some die in that case (programs might want to do that to stay interactive), so if you are using this from a module, never require a blocking wait. Instead, let the caller decide whether the call will block or not (for example, by coupling condition variables with some kind of request results and supporting callbacks so the caller knows that getting the result will not block, while still supporting blocking waits if the caller so desires).
You can ensure that ->recv never blocks by setting a callback and only calling ->recv from within that callback (or at a later time). This will work even when the event loop does not support blocking waits otherwise.
(1)》Note that doing a blocking wait in a callback is not supported by any event loop
这句是指callback最好是不要有会 block的语句
(2)》Not all event models support a blocking wait
这句是讲什么,“不是所有事件模块都支持BLOCK WAIT”?
请问这里的block wait是指 $cv->recv吗
(3)》so if you are using this from a module, never require a blocking wait. Instead, let the caller decide whether the call will block or not
=》求说明
(4)》You can ensure that ->recv never blocks by setting a callback and only calling ->recv from within that callback (or at a later time). This will work even when the event loop does not support blocking waits otherwise
这句就比较绕了。
你可以通过这样的方式来确保->recv不会block: 设定一个callback,并在这个callback里只调用->recv.
作为我这个新手来讲,都是在main programe里显示调用$cv->recv来开启loop的。
试验了一下,在callback里调用$cv->recv好像“不起作用”,event loop不会开启。那第(4)句中的方法,->recv是不会BLOCK了,但EVENT LOOP会启动起来么?
|
|