… | |
… | |
181 | my variables are only visible after the statement in which they are |
181 | my variables are only visible after the statement in which they are |
182 | declared. |
182 | declared. |
183 | |
183 | |
184 | =head2 I/O WATCHERS |
184 | =head2 I/O WATCHERS |
185 | |
185 | |
|
|
186 | $w = AnyEvent->io ( |
|
|
187 | fh => <filehandle_or_fileno>, |
|
|
188 | poll => <"r" or "w">, |
|
|
189 | cb => <callback>, |
|
|
190 | ); |
|
|
191 | |
186 | You can create an I/O watcher by calling the C<< AnyEvent->io >> method |
192 | You can create an I/O watcher by calling the C<< AnyEvent->io >> method |
187 | with the following mandatory key-value pairs as arguments: |
193 | with the following mandatory key-value pairs as arguments: |
188 | |
194 | |
189 | C<fh> is the Perl I<file handle> (or a naked file descriptor) to watch |
195 | C<fh> is the Perl I<file handle> (or a naked file descriptor) to watch |
190 | for events (AnyEvent might or might not keep a reference to this file |
196 | for events (AnyEvent might or might not keep a reference to this file |
… | |
… | |
219 | undef $w; |
225 | undef $w; |
220 | }); |
226 | }); |
221 | |
227 | |
222 | =head2 TIME WATCHERS |
228 | =head2 TIME WATCHERS |
223 | |
229 | |
|
|
230 | $w = AnyEvent->timer (after => <seconds>, cb => <callback>); |
|
|
231 | |
|
|
232 | $w = AnyEvent->timer ( |
|
|
233 | after => <fractional_seconds>, |
|
|
234 | interval => <fractional_seconds>, |
|
|
235 | cb => <callback>, |
|
|
236 | ); |
|
|
237 | |
224 | You can create a time watcher by calling the C<< AnyEvent->timer >> |
238 | You can create a time watcher by calling the C<< AnyEvent->timer >> |
225 | method with the following mandatory arguments: |
239 | method with the following mandatory arguments: |
226 | |
240 | |
227 | C<after> specifies after how many seconds (fractional values are |
241 | C<after> specifies after how many seconds (fractional values are |
228 | supported) the callback should be invoked. C<cb> is the callback to invoke |
242 | supported) the callback should be invoked. C<cb> is the callback to invoke |
… | |
… | |
354 | Note that updating the time I<might> cause some events to be handled. |
368 | Note that updating the time I<might> cause some events to be handled. |
355 | |
369 | |
356 | =back |
370 | =back |
357 | |
371 | |
358 | =head2 SIGNAL WATCHERS |
372 | =head2 SIGNAL WATCHERS |
|
|
373 | |
|
|
374 | $w = AnyEvent->signal (signal => <uppercase_signal_name>, cb => <callback>); |
359 | |
375 | |
360 | You can watch for signals using a signal watcher, C<signal> is the signal |
376 | You can watch for signals using a signal watcher, C<signal> is the signal |
361 | I<name> in uppercase and without any C<SIG> prefix, C<cb> is the Perl |
377 | I<name> in uppercase and without any C<SIG> prefix, C<cb> is the Perl |
362 | callback to be invoked whenever a signal occurs. |
378 | callback to be invoked whenever a signal occurs. |
363 | |
379 | |
… | |
… | |
397 | event loops such as L<Event> or L<Event::Lib> (and not with L<POE> |
413 | event loops such as L<Event> or L<Event::Lib> (and not with L<POE> |
398 | currently, as POE does it's own workaround with one-second latency). With |
414 | currently, as POE does it's own workaround with one-second latency). With |
399 | those, you just have to suffer the delays. |
415 | those, you just have to suffer the delays. |
400 | |
416 | |
401 | =head2 CHILD PROCESS WATCHERS |
417 | =head2 CHILD PROCESS WATCHERS |
|
|
418 | |
|
|
419 | $w = AnyEvent->child (pid => <process id>, cb => <callback>); |
402 | |
420 | |
403 | You can also watch on a child process exit and catch its exit status. |
421 | You can also watch on a child process exit and catch its exit status. |
404 | |
422 | |
405 | The child process is specified by the C<pid> argument (one some backends, |
423 | The child process is specified by the C<pid> argument (one some backends, |
406 | using C<0> watches for any child process exit, on others this will |
424 | using C<0> watches for any child process exit, on others this will |
… | |
… | |
455 | # do something else, then wait for process exit |
473 | # do something else, then wait for process exit |
456 | $done->recv; |
474 | $done->recv; |
457 | |
475 | |
458 | =head2 IDLE WATCHERS |
476 | =head2 IDLE WATCHERS |
459 | |
477 | |
|
|
478 | $w = AnyEvent->idle (cb => <callback>); |
|
|
479 | |
460 | Sometimes there is a need to do something, but it is not so important |
480 | Sometimes there is a need to do something, but it is not so important |
461 | to do it instantly, but only when there is nothing better to do. This |
481 | to do it instantly, but only when there is nothing better to do. This |
462 | "nothing better to do" is usually defined to be "no other events need |
482 | "nothing better to do" is usually defined to be "no other events need |
463 | attention by the event loop". |
483 | attention by the event loop". |
464 | |
484 | |
… | |
… | |
489 | } |
509 | } |
490 | }); |
510 | }); |
491 | }); |
511 | }); |
492 | |
512 | |
493 | =head2 CONDITION VARIABLES |
513 | =head2 CONDITION VARIABLES |
|
|
514 | |
|
|
515 | $cv = AnyEvent->condvar; |
|
|
516 | |
|
|
517 | $cv->send (<list>); |
|
|
518 | my @res = $cv->recv; |
494 | |
519 | |
495 | If you are familiar with some event loops you will know that all of them |
520 | If you are familiar with some event loops you will know that all of them |
496 | require you to run some blocking "loop", "run" or similar function that |
521 | require you to run some blocking "loop", "run" or similar function that |
497 | will actively watch for new events and call your callbacks. |
522 | will actively watch for new events and call your callbacks. |
498 | |
523 | |
… | |
… | |
1623 | |
1648 | |
1624 | # undocumented/compatibility with pre-3.4 |
1649 | # undocumented/compatibility with pre-3.4 |
1625 | *broadcast = \&send; |
1650 | *broadcast = \&send; |
1626 | *wait = \&_wait; |
1651 | *wait = \&_wait; |
1627 | |
1652 | |
|
|
1653 | ############################################################################# |
|
|
1654 | # "new" API, currently only emulation of it |
|
|
1655 | ############################################################################# |
|
|
1656 | |
|
|
1657 | package AE; |
|
|
1658 | |
|
|
1659 | sub io($$$) { |
|
|
1660 | AnyEvent->io (fh => $_[0], poll => $_[1] ? "w" : "r", cb => $_[2]) |
|
|
1661 | } |
|
|
1662 | |
|
|
1663 | sub timer($$$) { |
|
|
1664 | AnyEvent->timer (after => $_[0], interval => $_[1], cb => $_[2]); |
|
|
1665 | } |
|
|
1666 | |
|
|
1667 | sub signal($$) { |
|
|
1668 | AnyEvent->signal (signal => $_[0], cb => $_[1]); |
|
|
1669 | } |
|
|
1670 | |
|
|
1671 | sub child($$) { |
|
|
1672 | AnyEvent->child (pid => $_[0], cb => $_[1]); |
|
|
1673 | } |
|
|
1674 | |
|
|
1675 | sub idle($) { |
|
|
1676 | AnyEvent->idle (cb => $_[0]); |
|
|
1677 | } |
|
|
1678 | |
|
|
1679 | sub cv() { |
|
|
1680 | AnyEvent->condvar |
|
|
1681 | } |
|
|
1682 | |
|
|
1683 | sub now() { |
|
|
1684 | AnyEvent->now |
|
|
1685 | } |
|
|
1686 | |
|
|
1687 | sub now_update() { |
|
|
1688 | AnyEvent->now_update |
|
|
1689 | } |
|
|
1690 | |
|
|
1691 | sub time() { |
|
|
1692 | AnyEvent->time |
|
|
1693 | } |
|
|
1694 | |
1628 | =head1 ERROR AND EXCEPTION HANDLING |
1695 | =head1 ERROR AND EXCEPTION HANDLING |
1629 | |
1696 | |
1630 | In general, AnyEvent does not do any error handling - it relies on the |
1697 | In general, AnyEvent does not do any error handling - it relies on the |
1631 | caller to do that if required. The L<AnyEvent::Strict> module (see also |
1698 | caller to do that if required. The L<AnyEvent::Strict> module (see also |
1632 | the C<PERL_ANYEVENT_STRICT> environment variable, below) provides strict |
1699 | the C<PERL_ANYEVENT_STRICT> environment variable, below) provides strict |