… | |
… | |
576 | after => 1, |
576 | after => 1, |
577 | cb => sub { $result_ready->send }, |
577 | cb => sub { $result_ready->send }, |
578 | ); |
578 | ); |
579 | |
579 | |
580 | # this "blocks" (while handling events) till the callback |
580 | # this "blocks" (while handling events) till the callback |
581 | # calls -<send |
581 | # calls ->send |
582 | $result_ready->recv; |
582 | $result_ready->recv; |
583 | |
583 | |
584 | Example: wait for a timer, but take advantage of the fact that condition |
584 | Example: wait for a timer, but take advantage of the fact that condition |
585 | variables are also callable directly. |
585 | variables are also callable directly. |
586 | |
586 | |
… | |
… | |
1228 | warn "read: $input\n"; # output what has been read |
1228 | warn "read: $input\n"; # output what has been read |
1229 | $cv->send if $input =~ /^q/i; # quit program if /^q/i |
1229 | $cv->send if $input =~ /^q/i; # quit program if /^q/i |
1230 | }, |
1230 | }, |
1231 | ); |
1231 | ); |
1232 | |
1232 | |
1233 | my $time_watcher; # can only be used once |
|
|
1234 | |
|
|
1235 | sub new_timer { |
|
|
1236 | $timer = AnyEvent->timer (after => 1, cb => sub { |
1233 | my $time_watcher = AnyEvent->timer (after => 1, interval => 1, cb => sub { |
1237 | warn "timeout\n"; # print 'timeout' about every second |
1234 | warn "timeout\n"; # print 'timeout' at most every second |
1238 | &new_timer; # and restart the time |
|
|
1239 | }); |
|
|
1240 | } |
1235 | }); |
1241 | |
|
|
1242 | new_timer; # create first timer |
|
|
1243 | |
1236 | |
1244 | $cv->recv; # wait until user enters /^q/i |
1237 | $cv->recv; # wait until user enters /^q/i |
1245 | |
1238 | |
1246 | REAL-WORLD EXAMPLE |
1239 | REAL-WORLD EXAMPLE |
1247 | Consider the Net::FCP module. It features (among others) the following |
1240 | Consider the Net::FCP module. It features (among others) the following |
… | |
… | |
1665 | As you can see, the AnyEvent + EV combination even beats the |
1658 | As you can see, the AnyEvent + EV combination even beats the |
1666 | hand-optimised "raw sockets benchmark", while AnyEvent + its pure perl |
1659 | hand-optimised "raw sockets benchmark", while AnyEvent + its pure perl |
1667 | backend easily beats IO::Lambda and POE. |
1660 | backend easily beats IO::Lambda and POE. |
1668 | |
1661 | |
1669 | And even the 100% non-blocking version written using the high-level (and |
1662 | And even the 100% non-blocking version written using the high-level (and |
1670 | slow :) AnyEvent::Handle abstraction beats both POE and IO::Lambda by a |
1663 | slow :) AnyEvent::Handle abstraction beats both POE and IO::Lambda |
1671 | large margin, even though it does all of DNS, tcp-connect and socket I/O |
1664 | higher level ("unoptimised") abstractions by a large margin, even though |
1672 | in a non-blocking way. |
1665 | it does all of DNS, tcp-connect and socket I/O in a non-blocking way. |
1673 | |
1666 | |
1674 | The two AnyEvent benchmarks programs can be found as eg/ae0.pl and |
1667 | The two AnyEvent benchmarks programs can be found as eg/ae0.pl and |
1675 | eg/ae2.pl in the AnyEvent distribution, the remaining benchmarks are |
1668 | eg/ae2.pl in the AnyEvent distribution, the remaining benchmarks are |
1676 | part of the IO::lambda distribution and were used without any changes. |
1669 | part of the IO::Lambda distribution and were used without any changes. |
1677 | |
1670 | |
1678 | SIGNALS |
1671 | SIGNALS |
1679 | AnyEvent currently installs handlers for these signals: |
1672 | AnyEvent currently installs handlers for these signals: |
1680 | |
1673 | |
1681 | SIGCHLD |
1674 | SIGCHLD |
… | |
… | |
1749 | "AnyEvent::Util::guard". This speeds up guards considerably (and |
1742 | "AnyEvent::Util::guard". This speeds up guards considerably (and |
1750 | uses a lot less memory), but otherwise doesn't affect guard |
1743 | uses a lot less memory), but otherwise doesn't affect guard |
1751 | operation much. It is purely used for performance. |
1744 | operation much. It is purely used for performance. |
1752 | |
1745 | |
1753 | JSON and JSON::XS |
1746 | JSON and JSON::XS |
1754 | This module is required when you want to read or write JSON data via |
1747 | One of these modules is required when you want to read or write JSON |
1755 | AnyEvent::Handle. It is also written in pure-perl, but can take |
1748 | data via AnyEvent::Handle. It is also written in pure-perl, but can |
1756 | advantage of the ultra-high-speed JSON::XS module when it is |
1749 | take advantage of the ultra-high-speed JSON::XS module when it is |
1757 | installed. |
1750 | installed. |
1758 | |
1751 | |
1759 | In fact, AnyEvent::Handle will use JSON::XS by default if it is |
1752 | In fact, AnyEvent::Handle will use JSON::XS by default if it is |
1760 | installed. |
1753 | installed. |
1761 | |
1754 | |