… | |
… | |
353 | then this "current" time will differ substantially from the real |
353 | then this "current" time will differ substantially from the real |
354 | time, which might affect timers and time-outs. |
354 | time, which might affect timers and time-outs. |
355 | |
355 | |
356 | When this is the case, you can call this method, which will update |
356 | When this is the case, you can call this method, which will update |
357 | the event loop's idea of "current time". |
357 | the event loop's idea of "current time". |
|
|
358 | |
|
|
359 | A typical example would be a script in a web server (e.g. |
|
|
360 | "mod_perl") - when mod_perl executes the script, then the event loop |
|
|
361 | will have the wrong idea about the "current time" (being potentially |
|
|
362 | far in the past, when the script ran the last time). In that case |
|
|
363 | you should arrange a call to "AnyEvent->now_update" each time the |
|
|
364 | web server process wakes up again (e.g. at the start of your script, |
|
|
365 | or in a handler). |
358 | |
366 | |
359 | Note that updating the time *might* cause some events to be handled. |
367 | Note that updating the time *might* cause some events to be handled. |
360 | |
368 | |
361 | SIGNAL WATCHERS |
369 | SIGNAL WATCHERS |
362 | $w = AnyEvent->signal (signal => <uppercase_signal_name>, cb => <callback>); |
370 | $w = AnyEvent->signal (signal => <uppercase_signal_name>, cb => <callback>); |
… | |
… | |
576 | after => 1, |
584 | after => 1, |
577 | cb => sub { $result_ready->send }, |
585 | cb => sub { $result_ready->send }, |
578 | ); |
586 | ); |
579 | |
587 | |
580 | # this "blocks" (while handling events) till the callback |
588 | # this "blocks" (while handling events) till the callback |
581 | # calls -<send |
589 | # calls ->send |
582 | $result_ready->recv; |
590 | $result_ready->recv; |
583 | |
591 | |
584 | Example: wait for a timer, but take advantage of the fact that condition |
592 | Example: wait for a timer, but take advantage of the fact that condition |
585 | variables are also callable directly. |
593 | variables are also callable directly. |
586 | |
594 | |
… | |
… | |
643 | into one. For example, a function that pings many hosts in parallel |
651 | into one. For example, a function that pings many hosts in parallel |
644 | might want to use a condition variable for the whole process. |
652 | might want to use a condition variable for the whole process. |
645 | |
653 | |
646 | Every call to "->begin" will increment a counter, and every call to |
654 | Every call to "->begin" will increment a counter, and every call to |
647 | "->end" will decrement it. If the counter reaches 0 in "->end", the |
655 | "->end" will decrement it. If the counter reaches 0 in "->end", the |
648 | (last) callback passed to "begin" will be executed. That callback is |
656 | (last) callback passed to "begin" will be executed, passing the |
649 | *supposed* to call "->send", but that is not required. If no |
657 | condvar as first argument. That callback is *supposed* to call |
|
|
658 | "->send", but that is not required. If no group callback was set, |
650 | callback was set, "send" will be called without any arguments. |
659 | "send" will be called without any arguments. |
651 | |
660 | |
652 | You can think of "$cv->send" giving you an OR condition (one call |
661 | You can think of "$cv->send" giving you an OR condition (one call |
653 | sends), while "$cv->begin" and "$cv->end" giving you an AND |
662 | sends), while "$cv->begin" and "$cv->end" giving you an AND |
654 | condition (all "begin" calls must be "end"'ed before the condvar |
663 | condition (all "begin" calls must be "end"'ed before the condvar |
655 | sends). |
664 | sends). |
… | |
… | |
683 | that are begung can potentially be zero: |
692 | that are begung can potentially be zero: |
684 | |
693 | |
685 | my $cv = AnyEvent->condvar; |
694 | my $cv = AnyEvent->condvar; |
686 | |
695 | |
687 | my %result; |
696 | my %result; |
688 | $cv->begin (sub { $cv->send (\%result) }); |
697 | $cv->begin (sub { shift->send (\%result) }); |
689 | |
698 | |
690 | for my $host (@list_of_hosts) { |
699 | for my $host (@list_of_hosts) { |
691 | $cv->begin; |
700 | $cv->begin; |
692 | ping_host_then_call_callback $host, sub { |
701 | ping_host_then_call_callback $host, sub { |
693 | $result{$host} = ...; |
702 | $result{$host} = ...; |
… | |
… | |
1227 | warn "read: $input\n"; # output what has been read |
1236 | warn "read: $input\n"; # output what has been read |
1228 | $cv->send if $input =~ /^q/i; # quit program if /^q/i |
1237 | $cv->send if $input =~ /^q/i; # quit program if /^q/i |
1229 | }, |
1238 | }, |
1230 | ); |
1239 | ); |
1231 | |
1240 | |
1232 | my $time_watcher; # can only be used once |
|
|
1233 | |
|
|
1234 | sub new_timer { |
|
|
1235 | $timer = AnyEvent->timer (after => 1, cb => sub { |
1241 | my $time_watcher = AnyEvent->timer (after => 1, interval => 1, cb => sub { |
1236 | warn "timeout\n"; # print 'timeout' about every second |
1242 | warn "timeout\n"; # print 'timeout' at most every second |
1237 | &new_timer; # and restart the time |
|
|
1238 | }); |
|
|
1239 | } |
1243 | }); |
1240 | |
|
|
1241 | new_timer; # create first timer |
|
|
1242 | |
1244 | |
1243 | $cv->recv; # wait until user enters /^q/i |
1245 | $cv->recv; # wait until user enters /^q/i |
1244 | |
1246 | |
1245 | REAL-WORLD EXAMPLE |
1247 | REAL-WORLD EXAMPLE |
1246 | Consider the Net::FCP module. It features (among others) the following |
1248 | Consider the Net::FCP module. It features (among others) the following |
… | |
… | |
1664 | As you can see, the AnyEvent + EV combination even beats the |
1666 | As you can see, the AnyEvent + EV combination even beats the |
1665 | hand-optimised "raw sockets benchmark", while AnyEvent + its pure perl |
1667 | hand-optimised "raw sockets benchmark", while AnyEvent + its pure perl |
1666 | backend easily beats IO::Lambda and POE. |
1668 | backend easily beats IO::Lambda and POE. |
1667 | |
1669 | |
1668 | And even the 100% non-blocking version written using the high-level (and |
1670 | And even the 100% non-blocking version written using the high-level (and |
1669 | slow :) AnyEvent::Handle abstraction beats both POE and IO::Lambda by a |
1671 | slow :) AnyEvent::Handle abstraction beats both POE and IO::Lambda |
1670 | large margin, even though it does all of DNS, tcp-connect and socket I/O |
1672 | higher level ("unoptimised") abstractions by a large margin, even though |
1671 | in a non-blocking way. |
1673 | it does all of DNS, tcp-connect and socket I/O in a non-blocking way. |
1672 | |
1674 | |
1673 | The two AnyEvent benchmarks programs can be found as eg/ae0.pl and |
1675 | The two AnyEvent benchmarks programs can be found as eg/ae0.pl and |
1674 | eg/ae2.pl in the AnyEvent distribution, the remaining benchmarks are |
1676 | eg/ae2.pl in the AnyEvent distribution, the remaining benchmarks are |
1675 | part of the IO::lambda distribution and were used without any changes. |
1677 | part of the IO::Lambda distribution and were used without any changes. |
1676 | |
1678 | |
1677 | SIGNALS |
1679 | SIGNALS |
1678 | AnyEvent currently installs handlers for these signals: |
1680 | AnyEvent currently installs handlers for these signals: |
1679 | |
1681 | |
1680 | SIGCHLD |
1682 | SIGCHLD |
… | |
… | |
1748 | "AnyEvent::Util::guard". This speeds up guards considerably (and |
1750 | "AnyEvent::Util::guard". This speeds up guards considerably (and |
1749 | uses a lot less memory), but otherwise doesn't affect guard |
1751 | uses a lot less memory), but otherwise doesn't affect guard |
1750 | operation much. It is purely used for performance. |
1752 | operation much. It is purely used for performance. |
1751 | |
1753 | |
1752 | JSON and JSON::XS |
1754 | JSON and JSON::XS |
1753 | This module is required when you want to read or write JSON data via |
1755 | One of these modules is required when you want to read or write JSON |
1754 | AnyEvent::Handle. It is also written in pure-perl, but can take |
1756 | data via AnyEvent::Handle. It is also written in pure-perl, but can |
1755 | advantage of the ultra-high-speed JSON::XS module when it is |
1757 | take advantage of the ultra-high-speed JSON::XS module when it is |
1756 | installed. |
1758 | installed. |
1757 | |
1759 | |
1758 | In fact, AnyEvent::Handle will use JSON::XS by default if it is |
1760 | In fact, AnyEvent::Handle will use JSON::XS by default if it is |
1759 | installed. |
1761 | installed. |
1760 | |
1762 | |