亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫(kù)
最近訪問(wèn)板塊 發(fā)新帖
查看: 4754 | 回復(fù): 4
打印 上一主題 下一主題

請(qǐng)教AnyEvent [復(fù)制鏈接]

論壇徽章:
1
雙子座
日期:2013-11-06 17:18:01
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2013-12-25 15:31 |只看該作者 |倒序?yàn)g覽
本帖最后由 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)么?

論壇徽章:
1
2015年辭舊歲徽章
日期:2015-03-03 16:54:15
2 [報(bào)告]
發(fā)表于 2013-12-25 20:09 |只看該作者
1. 作者哪說(shuō)“最好是不要有”了?。原話中顯然沒(méi)有這個(gè)意思。callback中不能有阻塞等待
2. 這段的語(yǔ)言環(huán)境就是在說(shuō)recv,blocking wait指的就是recv
3. 他這是再說(shuō)寫(xiě)事件模塊呢,如果是anyevent的使用者,可以忽略這塊。
4. 這句的意思是,recv是阻塞的,但你可以用->cb建立一個(gè)回調(diào),這樣你就不必等這個(gè)recv了。這有點(diǎn)像很多的->recv在一起不會(huì)阻塞

論壇徽章:
1
雙子座
日期:2013-11-06 17:18:01
3 [報(bào)告]
發(fā)表于 2013-12-26 09:05 |只看該作者
py 發(fā)表于 2013-12-25 20:09
1. 作者哪說(shuō)“最好是不要有”了?。原話中顯然沒(méi)有這個(gè)意思。callback中不能有阻塞等待
2. 這段的語(yǔ)言環(huán)境 ...

4. 這句的意思是,recv是阻塞的,但你可以用->cb建立一個(gè)回調(diào),這樣你就不必等這個(gè)recv了。這有點(diǎn)像很多的->recv在一起不會(huì)阻塞
》這句話還是沒(méi)明白,能否舉例說(shuō)明一下。

論壇徽章:
1
雙子座
日期:2013-11-06 17:18:01
4 [報(bào)告]
發(fā)表于 2013-12-26 10:29 |只看該作者
回復(fù) 2# py
》4. 這句的意思是,recv是阻塞的,但你可以用->cb建立一個(gè)回調(diào),這樣你就不必等這個(gè)recv了。這有點(diǎn)像很多的->recv在一起不會(huì)阻塞
是指如下描述嗎?
$cb = $cv->cb ($cb->($cv))
This is a mutator function that returns the callback set and optionally replaces it before doing so.

The callback will be called when the condition becomes "true", i.e. when send or croak are called, with the only argument being the condition variable itself. If the condition is already true, the callback is called immediately when it is set. Calling recv inside the callback or at any later time is guaranteed not to block.

具體到代碼就是如下的方案1?

  1. use AnyEvent;
  2. use AnyEvent::Handle;

  3. my $cv = AnyEvent->condvar;

  4. my $io_watcher = AnyEvent->io(
  5.         fh => \*STDIN,
  6.         poll =>'r',
  7.         cb => sub {
  8.                 warn " io event <$_[0]>\n";
  9.                 chomp(my $input = <STDIN>);
  10.                 warn "read:$input\n";
  11.                 $cv->send if $input=~/^q/i;
  12.         }
  13.      );

  14. my $time_watcher = AnyEvent->timer(after=>1,interval=>1,
  15.                         cb=> sub{
  16.                         warn "timeout\n";
  17.                         });

  18. #方案1
  19. #$cv->cb(sub {shift->recv;undef $time_watcher});

  20. #方案2
  21. #$cv->recv;

  22. # do something else.....


  23. #end of program

復(fù)制代碼
針對(duì)上述代碼,有個(gè)疑問(wèn),如果#do something else沒(méi)有任何實(shí)質(zhì)內(nèi)容。將->recv放置于callback,而非main program的話。
是不是整個(gè)program就馬上退出了?事件“來(lái)不急”觸發(fā)

論壇徽章:
1
2015年辭舊歲徽章
日期:2015-03-03 16:54:15
5 [報(bào)告]
發(fā)表于 2013-12-26 19:17 |只看該作者
我覺(jué)得他說(shuō)的不是這個(gè)意思。

而是兩個(gè)事件(兩個(gè)cv),cv1的recv如果直接寫(xiě)在cv2的callback中就會(huì)發(fā)生阻塞報(bào)錯(cuò),這個(gè)時(shí)候就要把cv1用cb包裹起來(lái),從而避免阻塞報(bào)錯(cuò)。
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專(zhuān)區(qū)
中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過(guò)ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP