国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Perl > 正文

Perl AnyEvent中的watcher實例

2020-10-31 15:05:39
字體:
來源:轉載
供稿:網友

這幾天看了下perl的事件編程框架AnyEvent,重點參考了一下幾篇文章:

http://search.cpan.org/~mlehmann/AnyEvent-7.05/lib/AnyEvent.pm

http://search.cpan.org/~mlehmann/AnyEvent-7.05/lib/AnyEvent/Intro.pod

http://m.survivalescaperooms.com/article/55278.htm

1、什么是事件編程?

舉個簡單的例子,你瀏覽網頁的時候,你點擊一個圖片,蹭的彈出一個東西,你不點,那就在那里,等待一個人來點它。如果你寫過js,其實就是,你注冊了很多的時間比如click,dbclick,keybord,submit等,那么瀏覽器就起到幫我們去監聽這些事件的發生(Loop)。當有對應的事件發生的時候,我們也一般也設置了callback,比如onclick,onsubmit等,去響應這些事件,這基本就是事件編程的一個縮影了。

2、perl AnyEvent中的watcher

在AnyEvent中有5中watcher,分別是IO,timer,signal, child, idle.

2.1 io watcher

復制代碼 代碼如下:

#!/usr/bin/perl
 
use AnyEvent;
my $cv = AnyEvent->condvar;
 
#open my $file , '<' , 'test.txt' or die "$!" ;
open F , '<' , 'test.txt' or die "$!" ;
my $io_watcher = AnyEvent->io (
      fh   => *F,
      poll => 'r',
      cb   => sub {
         chomp (my $input = sysread F ,my $buf ,1024); # read a line
 
         warn "read: $buf/n"  if $input >0 ;       # output what has been read
         #$cv->send if /quit/ ; # quit program if /quit/i
      },
   );
 
$cv->recv; # wait until user enters /quit/i

timer watcher

AnyEvent 的timer的一部分其實像javascript的setInterval :

復制代碼 代碼如下:

#!/usr/bin/perl
 
use 5.016;
use AnyEvent ;
 
my $cv = AnyEvent->condvar ;
 
my $w = AnyEvent->timer(
    after => 0 ,  #多少秒之后觸發事件
    interval => 2 ,  #多少秒觸發事件
    cb => sub {
        say AnyEvent->time ," ",AnyEvent->now ;
 
    }
);
 
$cv->recv;

signal watcher

前面我們在的文章中寫到了perl中對于信號的處理 《perl信號處理簡單學習》,這里主要是AnyEvent中對于這些事件的處理。

復制代碼 代碼如下:

#!/usr/bin/perl
 
use 5.016;
use AnyEvent ;
#say for keys %SIG; 看一下又多少信號
my $cv = AnyEvent->condvar ;
 
my $w = AnyEvent->signal(
    signal => 'INT',
    cb => sub {
        say AnyEvent->time ," ",AnyEvent->now ;
        exit 1 ;
 
    }
);
 
$cv->recv;

child watcher

復制代碼 代碼如下:

#!/usr/bin/perl
use AnyEvent;
   my $done = AnyEvent->condvar;
 
   my $pid = fork or exit 5;
 
   my $w = AnyEvent->child (
      pid => $pid,
      cb  => sub {
         my ($pid, $status) = @_;
         warn "pid $pid exited with status $status";
         $done->send;
      },
   );
 
   # do something else, then wait for process exit
   $done->recv;

idle watcher

就是如果main loop在空閑的時候做些什么呢?

復制代碼 代碼如下:

#!/usr/bin/perl
use AnyEvent;
   my @lines; # read data
   my $idle_w;
   $cv = AnyEvent->condvar;
   my $io_w = AnyEvent->io (fh => /*STDIN, poll => 'r', cb => sub {
      push @lines, scalar <STDIN>;
 
      # start an idle watcher, if not already done
      $idle_w ||= AnyEvent->idle (cb => sub {
         # handle only one line, when there are lines left
         if (my $line = shift @lines) {
            print "handled when idle: $line";
         } else {
            # otherwise disable the idle watcher again
            undef $idle_w;
         }
      });
   });
 
   $cv->recv;

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 青阳县| 宜君县| 屯昌县| 大化| 敦煌市| 剑川县| 辛集市| 怀柔区| 仪陇县| 广水市| 屏山县| 马公市| 滦平县| 深泽县| 肃南| 乌什县| 巴楚县| 博野县| 察哈| 桦川县| 德安县| 武冈市| 独山县| 克拉玛依市| 陇西县| 宜州市| 宜城市| 宁都县| 涿鹿县| 交口县| 西昌市| 和顺县| 聊城市| 贡觉县| 大兴区| 新乡县| 微山县| 唐山市| 漯河市| 两当县| 邯郸市|