- 論壇徽章:
- 1
|
本帖最后由 seufy88 于 2013-12-25 16:06 編輯
最近在看AnyEvent
在講解到$cv->recv時(shí),有幾句話不是很懂。
$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最好是不要有會(huì) block的語(yǔ)句
(2)》Not all event models support a blocking wait
這句是講什么,“不是所有事件模塊都支持BLOCK WAIT”?
請(qǐng)問(wèn)這里的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
=》求說(shuō)明
(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
這句就比較繞了。
你可以通過(guò)這樣的方式來(lái)確保->recv不會(huì)block: 設(shè)定一個(gè)callback,并在這個(gè)callback里只調(diào)用->recv.
作為我這個(gè)新手來(lái)講,都是在main programe里顯示調(diào)用$cv->recv來(lái)開(kāi)啟loop的。
試驗(yàn)了一下,在callback里調(diào)用$cv->recv好像“不起作用”,event loop不會(huì)開(kāi)啟。那第(4)句中的方法,->recv是不會(huì)BLOCK了,但EVENT LOOP會(huì)啟動(dòng)起來(lái)么?
|
|