ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/README
(Generate patch)

Comparing AnyEvent/README (file contents):
Revision 1.37 by root, Mon Apr 20 14:34:18 2009 UTC vs.
Revision 1.38 by root, Sun Apr 26 18:12:53 2009 UTC

5 loops 5 loops
6 6
7SYNOPSIS 7SYNOPSIS
8 use AnyEvent; 8 use AnyEvent;
9 9
10 # file descriptor readable
10 my $w = AnyEvent->io (fh => $fh, poll => "r|w", cb => sub { ... }); 11 my $w = AnyEvent->io (fh => $fh, poll => "r", cb => sub { ... });
11 12
13 # one-shot or repeating timers
12 my $w = AnyEvent->timer (after => $seconds, cb => sub { ... }); 14 my $w = AnyEvent->timer (after => $seconds, cb => sub { ... });
13 my $w = AnyEvent->timer (after => $seconds, interval => $seconds, cb => ... 15 my $w = AnyEvent->timer (after => $seconds, interval => $seconds, cb => ...
14 16
15 print AnyEvent->now; # prints current event loop time 17 print AnyEvent->now; # prints current event loop time
16 print AnyEvent->time; # think Time::HiRes::time or simply CORE::time. 18 print AnyEvent->time; # think Time::HiRes::time or simply CORE::time.
17 19
20 # POSIX signal
18 my $w = AnyEvent->signal (signal => "TERM", cb => sub { ... }); 21 my $w = AnyEvent->signal (signal => "TERM", cb => sub { ... });
19 22
23 # child process exit
20 my $w = AnyEvent->child (pid => $pid, cb => sub { 24 my $w = AnyEvent->child (pid => $pid, cb => sub {
21 my ($pid, $status) = @_; 25 my ($pid, $status) = @_;
22 ... 26 ...
23 }); 27 });
28
29 # called when event loop idle (if applicable)
30 my $w = AnyEvent->idle (cb => sub { ... });
24 31
25 my $w = AnyEvent->condvar; # stores whether a condition was flagged 32 my $w = AnyEvent->condvar; # stores whether a condition was flagged
26 $w->send; # wake up current and all future recv's 33 $w->send; # wake up current and all future recv's
27 $w->recv; # enters "main loop" till $condvar gets ->send 34 $w->recv; # enters "main loop" till $condvar gets ->send
28 # use a condvar in callback mode: 35 # use a condvar in callback mode:
400 ); 407 );
401 408
402 # do something else, then wait for process exit 409 # do something else, then wait for process exit
403 $done->recv; 410 $done->recv;
404 411
412 IDLE WATCHERS
413 Sometimes there is a need to do something, but it is not so important to
414 do it instantly, but only when there is nothing better to do. This
415 "nothing better to do" is usually defined to be "no other events need
416 attention by the event loop".
417
418 Idle watchers ideally get invoked when the event loop has nothing better
419 to do, just before it would block the process to wait for new events.
420 Instead of blocking, the idle watcher is invoked.
421
422 Most event loops unfortunately do not really support idle watchers (only
423 EV, Event and Glib do it in a usable fashion) - for the rest, AnyEvent
424 will simply call the callback "from time to time".
425
426 Example: read lines from STDIN, but only process them when the program
427 is otherwise idle:
428
429 my @lines; # read data
430 my $idle_w;
431 my $io_w = AnyEvent->io (fh => \*STDIN, poll => 'r', cb => sub {
432 push @lines, scalar <STDIN>;
433
434 # start an idle watcher, if not already done
435 $idle_w ||= AnyEvent->idle (cb => sub {
436 # handle only one line, when there are lines left
437 if (my $line = shift @lines) {
438 print "handled when idle: $line";
439 } else {
440 # otherwise disable the idle watcher again
441 undef $idle_w;
442 }
443 });
444 });
445
405 CONDITION VARIABLES 446 CONDITION VARIABLES
406 If you are familiar with some event loops you will know that all of them 447 If you are familiar with some event loops you will know that all of them
407 require you to run some blocking "loop", "run" or similar function that 448 require you to run some blocking "loop", "run" or similar function that
408 will actively watch for new events and call your callbacks. 449 will actively watch for new events and call your callbacks.
409 450

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines