… | |
… | |
6 | |
6 | |
7 | =head1 SYNOPSIS |
7 | =head1 SYNOPSIS |
8 | |
8 | |
9 | use AnyEvent; |
9 | use AnyEvent; |
10 | |
10 | |
|
|
11 | # file descriptor readable |
11 | my $w = AnyEvent->io (fh => $fh, poll => "r|w", cb => sub { ... }); |
12 | my $w = AnyEvent->io (fh => $fh, poll => "r", cb => sub { ... }); |
12 | |
13 | |
|
|
14 | # one-shot or repeating timers |
13 | my $w = AnyEvent->timer (after => $seconds, cb => sub { ... }); |
15 | my $w = AnyEvent->timer (after => $seconds, cb => sub { ... }); |
14 | my $w = AnyEvent->timer (after => $seconds, interval => $seconds, cb => ... |
16 | my $w = AnyEvent->timer (after => $seconds, interval => $seconds, cb => ... |
15 | |
17 | |
16 | print AnyEvent->now; # prints current event loop time |
18 | print AnyEvent->now; # prints current event loop time |
17 | print AnyEvent->time; # think Time::HiRes::time or simply CORE::time. |
19 | print AnyEvent->time; # think Time::HiRes::time or simply CORE::time. |
18 | |
20 | |
|
|
21 | # POSIX signal |
19 | my $w = AnyEvent->signal (signal => "TERM", cb => sub { ... }); |
22 | my $w = AnyEvent->signal (signal => "TERM", cb => sub { ... }); |
20 | |
23 | |
|
|
24 | # child process exit |
21 | my $w = AnyEvent->child (pid => $pid, cb => sub { |
25 | my $w = AnyEvent->child (pid => $pid, cb => sub { |
22 | my ($pid, $status) = @_; |
26 | my ($pid, $status) = @_; |
23 | ... |
27 | ... |
24 | }); |
28 | }); |
|
|
29 | |
|
|
30 | # called when event loop idle (if applicable) |
|
|
31 | my $w = AnyEvent->idle (cb => sub { ... }); |
25 | |
32 | |
26 | my $w = AnyEvent->condvar; # stores whether a condition was flagged |
33 | my $w = AnyEvent->condvar; # stores whether a condition was flagged |
27 | $w->send; # wake up current and all future recv's |
34 | $w->send; # wake up current and all future recv's |
28 | $w->recv; # enters "main loop" till $condvar gets ->send |
35 | $w->recv; # enters "main loop" till $condvar gets ->send |
29 | # use a condvar in callback mode: |
36 | # use a condvar in callback mode: |
… | |
… | |
319 | |
326 | |
320 | In either case, if you care (and in most cases, you don't), then you |
327 | In either case, if you care (and in most cases, you don't), then you |
321 | can get whatever behaviour you want with any event loop, by taking the |
328 | can get whatever behaviour you want with any event loop, by taking the |
322 | difference between C<< AnyEvent->time >> and C<< AnyEvent->now >> into |
329 | difference between C<< AnyEvent->time >> and C<< AnyEvent->now >> into |
323 | account. |
330 | account. |
|
|
331 | |
|
|
332 | =item AnyEvent->now_update |
|
|
333 | |
|
|
334 | Some event loops (such as L<EV> or L<AnyEvent::Impl::Perl>) cache |
|
|
335 | the current time for each loop iteration (see the discussion of L<< |
|
|
336 | AnyEvent->now >>, above). |
|
|
337 | |
|
|
338 | When a callback runs for a long time (or when the process sleeps), then |
|
|
339 | this "current" time will differ substantially from the real time, which |
|
|
340 | might affect timers and time-outs. |
|
|
341 | |
|
|
342 | When this is the case, you can call this method, which will update the |
|
|
343 | event loop's idea of "current time". |
|
|
344 | |
|
|
345 | Note that updating the time I<might> cause some events to be handled. |
324 | |
346 | |
325 | =back |
347 | =back |
326 | |
348 | |
327 | =head2 SIGNAL WATCHERS |
349 | =head2 SIGNAL WATCHERS |
328 | |
350 | |
… | |
… | |
395 | ); |
417 | ); |
396 | |
418 | |
397 | # do something else, then wait for process exit |
419 | # do something else, then wait for process exit |
398 | $done->recv; |
420 | $done->recv; |
399 | |
421 | |
|
|
422 | =head2 IDLE WATCHERS |
|
|
423 | |
|
|
424 | Sometimes there is a need to do something, but it is not so important |
|
|
425 | to do it instantly, but only when there is nothing better to do. This |
|
|
426 | "nothing better to do" is usually defined to be "no other events need |
|
|
427 | attention by the event loop". |
|
|
428 | |
|
|
429 | Idle watchers ideally get invoked when the event loop has nothing |
|
|
430 | better to do, just before it would block the process to wait for new |
|
|
431 | events. Instead of blocking, the idle watcher is invoked. |
|
|
432 | |
|
|
433 | Most event loops unfortunately do not really support idle watchers (only |
|
|
434 | EV, Event and Glib do it in a usable fashion) - for the rest, AnyEvent |
|
|
435 | will simply call the callback "from time to time". |
|
|
436 | |
|
|
437 | Example: read lines from STDIN, but only process them when the |
|
|
438 | program is otherwise idle: |
|
|
439 | |
|
|
440 | my @lines; # read data |
|
|
441 | my $idle_w; |
|
|
442 | my $io_w = AnyEvent->io (fh => \*STDIN, poll => 'r', cb => sub { |
|
|
443 | push @lines, scalar <STDIN>; |
|
|
444 | |
|
|
445 | # start an idle watcher, if not already done |
|
|
446 | $idle_w ||= AnyEvent->idle (cb => sub { |
|
|
447 | # handle only one line, when there are lines left |
|
|
448 | if (my $line = shift @lines) { |
|
|
449 | print "handled when idle: $line"; |
|
|
450 | } else { |
|
|
451 | # otherwise disable the idle watcher again |
|
|
452 | undef $idle_w; |
|
|
453 | } |
|
|
454 | }); |
|
|
455 | }); |
|
|
456 | |
400 | =head2 CONDITION VARIABLES |
457 | =head2 CONDITION VARIABLES |
401 | |
458 | |
402 | If you are familiar with some event loops you will know that all of them |
459 | If you are familiar with some event loops you will know that all of them |
403 | require you to run some blocking "loop", "run" or similar function that |
460 | require you to run some blocking "loop", "run" or similar function that |
404 | will actively watch for new events and call your callbacks. |
461 | will actively watch for new events and call your callbacks. |
… | |
… | |
873 | no warnings; |
930 | no warnings; |
874 | use strict qw(vars subs); |
931 | use strict qw(vars subs); |
875 | |
932 | |
876 | use Carp; |
933 | use Carp; |
877 | |
934 | |
878 | our $VERSION = 4.351; |
935 | our $VERSION = 4.4; |
879 | our $MODEL; |
936 | our $MODEL; |
880 | |
937 | |
881 | our $AUTOLOAD; |
938 | our $AUTOLOAD; |
882 | our @ISA; |
939 | our @ISA; |
883 | |
940 | |
… | |
… | |
915 | [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza |
972 | [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza |
916 | [Wx:: => AnyEvent::Impl::POE::], |
973 | [Wx:: => AnyEvent::Impl::POE::], |
917 | [Prima:: => AnyEvent::Impl::POE::], |
974 | [Prima:: => AnyEvent::Impl::POE::], |
918 | ); |
975 | ); |
919 | |
976 | |
920 | our %method = map +($_ => 1), qw(io timer time now signal child condvar one_event DESTROY); |
977 | our %method = map +($_ => 1), |
|
|
978 | qw(io timer time now now_update signal child idle condvar one_event DESTROY); |
921 | |
979 | |
922 | our @post_detect; |
980 | our @post_detect; |
923 | |
981 | |
924 | sub post_detect(&) { |
982 | sub post_detect(&) { |
925 | my ($cb) = @_; |
983 | my ($cb) = @_; |
… | |
… | |
930 | 1 |
988 | 1 |
931 | } else { |
989 | } else { |
932 | push @post_detect, $cb; |
990 | push @post_detect, $cb; |
933 | |
991 | |
934 | defined wantarray |
992 | defined wantarray |
935 | ? bless \$cb, "AnyEvent::Util::PostDetect" |
993 | ? bless \$cb, "AnyEvent::Util::postdetect" |
936 | : () |
994 | : () |
937 | } |
995 | } |
938 | } |
996 | } |
939 | |
997 | |
940 | sub AnyEvent::Util::PostDetect::DESTROY { |
998 | sub AnyEvent::Util::postdetect::DESTROY { |
941 | @post_detect = grep $_ != ${$_[0]}, @post_detect; |
999 | @post_detect = grep $_ != ${$_[0]}, @post_detect; |
942 | } |
1000 | } |
943 | |
1001 | |
944 | sub detect() { |
1002 | sub detect() { |
945 | unless ($MODEL) { |
1003 | unless ($MODEL) { |
… | |
… | |
982 | last; |
1040 | last; |
983 | } |
1041 | } |
984 | } |
1042 | } |
985 | |
1043 | |
986 | $MODEL |
1044 | $MODEL |
987 | or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: EV, Event or Glib."; |
1045 | or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: EV, Event or Glib.\n"; |
988 | } |
1046 | } |
989 | } |
1047 | } |
990 | |
1048 | |
991 | push @{"$MODEL\::ISA"}, "AnyEvent::Base"; |
1049 | push @{"$MODEL\::ISA"}, "AnyEvent::Base"; |
992 | |
1050 | |
… | |
… | |
1022 | my ($rw, $mode) = $poll eq "r" ? ($r, "<") |
1080 | my ($rw, $mode) = $poll eq "r" ? ($r, "<") |
1023 | : $poll eq "w" ? ($w, ">") |
1081 | : $poll eq "w" ? ($w, ">") |
1024 | : Carp::croak "AnyEvent->io requires poll set to either 'r' or 'w'"; |
1082 | : Carp::croak "AnyEvent->io requires poll set to either 'r' or 'w'"; |
1025 | |
1083 | |
1026 | open my $fh2, "$mode&" . fileno $fh |
1084 | open my $fh2, "$mode&" . fileno $fh |
1027 | or die "cannot dup() filehandle: $!"; |
1085 | or die "cannot dup() filehandle: $!,"; |
1028 | |
1086 | |
1029 | # we assume CLOEXEC is already set by perl in all important cases |
1087 | # we assume CLOEXEC is already set by perl in all important cases |
1030 | |
1088 | |
1031 | ($fh2, $rw) |
1089 | ($fh2, $rw) |
1032 | } |
1090 | } |
1033 | |
1091 | |
1034 | package AnyEvent::Base; |
1092 | package AnyEvent::Base; |
1035 | |
1093 | |
1036 | # default implementation for now and time |
1094 | # default implementations for many methods |
1037 | |
1095 | |
1038 | BEGIN { |
1096 | BEGIN { |
1039 | if (eval "use Time::HiRes (); time (); 1") { |
1097 | if (eval "use Time::HiRes (); Time::HiRes::time (); 1") { |
1040 | *_time = \&Time::HiRes::time; |
1098 | *_time = \&Time::HiRes::time; |
1041 | # if (eval "use POSIX (); (POSIX::times())... |
1099 | # if (eval "use POSIX (); (POSIX::times())... |
1042 | } else { |
1100 | } else { |
1043 | *_time = sub { time }; # epic fail |
1101 | *_time = sub { time }; # epic fail |
1044 | } |
1102 | } |
1045 | } |
1103 | } |
1046 | |
1104 | |
1047 | sub time { _time } |
1105 | sub time { _time } |
1048 | sub now { _time } |
1106 | sub now { _time } |
|
|
1107 | sub now_update { } |
1049 | |
1108 | |
1050 | # default implementation for ->condvar |
1109 | # default implementation for ->condvar |
1051 | |
1110 | |
1052 | sub condvar { |
1111 | sub condvar { |
1053 | bless { @_ == 3 ? (_ae_cb => $_[2]) : () }, AnyEvent::CondVar:: |
1112 | bless { @_ == 3 ? (_ae_cb => $_[2]) : () }, "AnyEvent::CondVar" |
1054 | } |
1113 | } |
1055 | |
1114 | |
1056 | # default implementation for ->signal |
1115 | # default implementation for ->signal |
1057 | |
1116 | |
1058 | our ($SIGPIPE_R, $SIGPIPE_W, %SIG_CB, %SIG_EV, $SIG_IO); |
1117 | our ($SIGPIPE_R, $SIGPIPE_W, %SIG_CB, %SIG_EV, $SIG_IO); |
… | |
… | |
1104 | local $!; |
1163 | local $!; |
1105 | syswrite $SIGPIPE_W, "\x00", 1 unless %SIG_EV; |
1164 | syswrite $SIGPIPE_W, "\x00", 1 unless %SIG_EV; |
1106 | undef $SIG_EV{$signal}; |
1165 | undef $SIG_EV{$signal}; |
1107 | }; |
1166 | }; |
1108 | |
1167 | |
1109 | bless [$signal, $arg{cb}], "AnyEvent::Base::Signal" |
1168 | bless [$signal, $arg{cb}], "AnyEvent::Base::signal" |
1110 | } |
1169 | } |
1111 | |
1170 | |
1112 | sub AnyEvent::Base::Signal::DESTROY { |
1171 | sub AnyEvent::Base::signal::DESTROY { |
1113 | my ($signal, $cb) = @{$_[0]}; |
1172 | my ($signal, $cb) = @{$_[0]}; |
1114 | |
1173 | |
1115 | delete $SIG_CB{$signal}{$cb}; |
1174 | delete $SIG_CB{$signal}{$cb}; |
1116 | |
1175 | |
1117 | delete $SIG{$signal} unless keys %{ $SIG_CB{$signal} }; |
1176 | delete $SIG{$signal} unless keys %{ $SIG_CB{$signal} }; |
… | |
… | |
1158 | $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_sigchld); |
1217 | $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_sigchld); |
1159 | # child could be a zombie already, so make at least one round |
1218 | # child could be a zombie already, so make at least one round |
1160 | &_sigchld; |
1219 | &_sigchld; |
1161 | } |
1220 | } |
1162 | |
1221 | |
1163 | bless [$pid, $arg{cb}], "AnyEvent::Base::Child" |
1222 | bless [$pid, $arg{cb}], "AnyEvent::Base::child" |
1164 | } |
1223 | } |
1165 | |
1224 | |
1166 | sub AnyEvent::Base::Child::DESTROY { |
1225 | sub AnyEvent::Base::child::DESTROY { |
1167 | my ($pid, $cb) = @{$_[0]}; |
1226 | my ($pid, $cb) = @{$_[0]}; |
1168 | |
1227 | |
1169 | delete $PID_CB{$pid}{$cb}; |
1228 | delete $PID_CB{$pid}{$cb}; |
1170 | delete $PID_CB{$pid} unless keys %{ $PID_CB{$pid} }; |
1229 | delete $PID_CB{$pid} unless keys %{ $PID_CB{$pid} }; |
1171 | |
1230 | |
1172 | undef $CHLD_W unless keys %PID_CB; |
1231 | undef $CHLD_W unless keys %PID_CB; |
|
|
1232 | } |
|
|
1233 | |
|
|
1234 | # idle emulation is done by simply using a timer, regardless |
|
|
1235 | # of whether the proces sis idle or not, and not letting |
|
|
1236 | # the callback use more than 50% of the time. |
|
|
1237 | sub idle { |
|
|
1238 | my (undef, %arg) = @_; |
|
|
1239 | |
|
|
1240 | my ($cb, $w, $rcb) = $arg{cb}; |
|
|
1241 | |
|
|
1242 | $rcb = sub { |
|
|
1243 | if ($cb) { |
|
|
1244 | $w = _time; |
|
|
1245 | &$cb; |
|
|
1246 | $w = _time - $w; |
|
|
1247 | |
|
|
1248 | # never use more then 50% of the time for the idle watcher, |
|
|
1249 | # within some limits |
|
|
1250 | $w = 0.0001 if $w < 0.0001; |
|
|
1251 | $w = 5 if $w > 5; |
|
|
1252 | |
|
|
1253 | $w = AnyEvent->timer (after => $w, cb => $rcb); |
|
|
1254 | } else { |
|
|
1255 | # clean up... |
|
|
1256 | undef $w; |
|
|
1257 | undef $rcb; |
|
|
1258 | } |
|
|
1259 | }; |
|
|
1260 | |
|
|
1261 | $w = AnyEvent->timer (after => 0.05, cb => $rcb); |
|
|
1262 | |
|
|
1263 | bless \\$cb, "AnyEvent::Base::idle" |
|
|
1264 | } |
|
|
1265 | |
|
|
1266 | sub AnyEvent::Base::idle::DESTROY { |
|
|
1267 | undef $${$_[0]}; |
1173 | } |
1268 | } |
1174 | |
1269 | |
1175 | package AnyEvent::CondVar; |
1270 | package AnyEvent::CondVar; |
1176 | |
1271 | |
1177 | our @ISA = AnyEvent::CondVar::Base::; |
1272 | our @ISA = AnyEvent::CondVar::Base::; |