… | |
… | |
271 | |
271 | |
272 | Example 2: fire an event after 0.5 seconds, then roughly every second. |
272 | Example 2: fire an event after 0.5 seconds, then roughly every second. |
273 | |
273 | |
274 | my $w = AnyEvent->timer (after => 0.5, interval => 1, cb => sub { |
274 | my $w = AnyEvent->timer (after => 0.5, interval => 1, cb => sub { |
275 | warn "timeout\n"; |
275 | warn "timeout\n"; |
276 | }; |
276 | }); |
277 | |
277 | |
278 | =head3 TIMING ISSUES |
278 | =head3 TIMING ISSUES |
279 | |
279 | |
280 | There are two ways to handle timers: based on real time (relative, "fire |
280 | There are two ways to handle timers: based on real time (relative, "fire |
281 | in 10 seconds") and based on wallclock time (absolute, "fire at 12 |
281 | in 10 seconds") and based on wallclock time (absolute, "fire at 12 |
… | |
… | |
763 | }; |
763 | }; |
764 | } |
764 | } |
765 | |
765 | |
766 | $cv->end; |
766 | $cv->end; |
767 | |
767 | |
|
|
768 | ... |
|
|
769 | |
|
|
770 | my $results = $cv->recv; |
|
|
771 | |
768 | This code fragment supposedly pings a number of hosts and calls |
772 | This code fragment supposedly pings a number of hosts and calls |
769 | C<send> after results for all then have have been gathered - in any |
773 | C<send> after results for all then have have been gathered - in any |
770 | order. To achieve this, the code issues a call to C<begin> when it starts |
774 | order. To achieve this, the code issues a call to C<begin> when it starts |
771 | each ping request and calls C<end> when it has received some result for |
775 | each ping request and calls C<end> when it has received some result for |
772 | it. Since C<begin> and C<end> only maintain a counter, the order in which |
776 | it. Since C<begin> and C<end> only maintain a counter, the order in which |
… | |
… | |
807 | |
811 | |
808 | In list context, all parameters passed to C<send> will be returned, |
812 | In list context, all parameters passed to C<send> will be returned, |
809 | in scalar context only the first one will be returned. |
813 | in scalar context only the first one will be returned. |
810 | |
814 | |
811 | Note that doing a blocking wait in a callback is not supported by any |
815 | Note that doing a blocking wait in a callback is not supported by any |
812 | event loop, that is, recursive invocation of a blocking C<< ->recv |
816 | event loop, that is, recursive invocation of a blocking C<< ->recv >> is |
813 | >> is not allowed, and the C<recv> call will C<croak> if such a |
817 | not allowed and the C<recv> call will C<croak> if such a condition is |
814 | condition is detected. This condition can be slightly loosened by using |
818 | detected. This requirement can be dropped by relying on L<Coro::AnyEvent> |
815 | L<Coro::AnyEvent>, which allows you to do a blocking C<< ->recv >> from |
819 | , which allows you to do a blocking C<< ->recv >> from any thread |
816 | any thread that doesn't run the event loop itself. |
820 | that doesn't run the event loop itself. L<Coro::AnyEvent> is loaded |
|
|
821 | automatically when L<Coro> is used with L<AnyEvent>, so code does not need |
|
|
822 | to do anything special to take advantage of that: any code that would |
|
|
823 | normally block your program because it calls C<recv>, be executed in an |
|
|
824 | C<async> thread instead without blocking other threads. |
817 | |
825 | |
818 | Not all event models support a blocking wait - some die in that case |
826 | Not all event models support a blocking wait - some die in that case |
819 | (programs might want to do that to stay interactive), so I<if you are |
827 | (programs might want to do that to stay interactive), so I<if you are |
820 | using this from a module, never require a blocking wait>. Instead, let the |
828 | using this from a module, never require a blocking wait>. Instead, let the |
821 | caller decide whether the call will block or not (for example, by coupling |
829 | caller decide whether the call will block or not (for example, by coupling |
… | |
… | |
1139 | a longer non-exhaustive list), and the list is heavily biased towards |
1147 | a longer non-exhaustive list), and the list is heavily biased towards |
1140 | modules of the AnyEvent author himself :) |
1148 | modules of the AnyEvent author himself :) |
1141 | |
1149 | |
1142 | =over 4 |
1150 | =over 4 |
1143 | |
1151 | |
1144 | =item L<AnyEvent::Util> |
1152 | =item L<AnyEvent::Util> (part of the AnyEvent distribution) |
1145 | |
1153 | |
1146 | Contains various utility functions that replace often-used blocking |
1154 | Contains various utility functions that replace often-used blocking |
1147 | functions such as C<inet_aton> with event/callback-based versions. |
1155 | functions such as C<inet_aton> with event/callback-based versions. |
1148 | |
1156 | |
1149 | =item L<AnyEvent::Socket> |
1157 | =item L<AnyEvent::Socket> (part of the AnyEvent distribution) |
1150 | |
1158 | |
1151 | Provides various utility functions for (internet protocol) sockets, |
1159 | Provides various utility functions for (internet protocol) sockets, |
1152 | addresses and name resolution. Also functions to create non-blocking tcp |
1160 | addresses and name resolution. Also functions to create non-blocking tcp |
1153 | connections or tcp servers, with IPv6 and SRV record support and more. |
1161 | connections or tcp servers, with IPv6 and SRV record support and more. |
1154 | |
1162 | |
1155 | =item L<AnyEvent::Handle> |
1163 | =item L<AnyEvent::Handle> (part of the AnyEvent distribution) |
1156 | |
1164 | |
1157 | Provide read and write buffers, manages watchers for reads and writes, |
1165 | Provide read and write buffers, manages watchers for reads and writes, |
1158 | supports raw and formatted I/O, I/O queued and fully transparent and |
1166 | supports raw and formatted I/O, I/O queued and fully transparent and |
1159 | non-blocking SSL/TLS (via L<AnyEvent::TLS>). |
1167 | non-blocking SSL/TLS (via L<AnyEvent::TLS>). |
1160 | |
1168 | |
1161 | =item L<AnyEvent::DNS> |
1169 | =item L<AnyEvent::DNS> (part of the AnyEvent distribution) |
1162 | |
1170 | |
1163 | Provides rich asynchronous DNS resolver capabilities. |
1171 | Provides rich asynchronous DNS resolver capabilities. |
1164 | |
1172 | |
1165 | =item L<AnyEvent::HTTP>, L<AnyEvent::IRC>, L<AnyEvent::XMPP>, L<AnyEvent::GPSD>, L<AnyEvent::IGS>, L<AnyEvent::FCP> |
1173 | =item L<AnyEvent::HTTP>, L<AnyEvent::IRC>, L<AnyEvent::XMPP>, L<AnyEvent::GPSD>, L<AnyEvent::IGS>, L<AnyEvent::FCP> |
1166 | |
1174 | |
1167 | Implement event-based interfaces to the protocols of the same name (for |
1175 | Implement event-based interfaces to the protocols of the same name (for |
1168 | the curious, IGS is the International Go Server and FCP is the Freenet |
1176 | the curious, IGS is the International Go Server and FCP is the Freenet |
1169 | Client Protocol). |
1177 | Client Protocol). |
1170 | |
1178 | |
1171 | =item L<AnyEvent::AIO> |
1179 | =item L<AnyEvent::AIO> (part of the AnyEvent distribution) |
1172 | |
1180 | |
1173 | Truly asynchronous (as opposed to non-blocking) I/O, should be in the |
1181 | Truly asynchronous (as opposed to non-blocking) I/O, should be in the |
1174 | toolbox of every event programmer. AnyEvent::AIO transparently fuses |
1182 | toolbox of every event programmer. AnyEvent::AIO transparently fuses |
1175 | L<IO::AIO> and AnyEvent together, giving AnyEvent access to event-based |
1183 | L<IO::AIO> and AnyEvent together, giving AnyEvent access to event-based |
1176 | file I/O, and much more. |
1184 | file I/O, and much more. |
… | |
… | |
1221 | |
1229 | |
1222 | =cut |
1230 | =cut |
1223 | |
1231 | |
1224 | package AnyEvent; |
1232 | package AnyEvent; |
1225 | |
1233 | |
1226 | # basically a tuned-down version of common::sense |
1234 | BEGIN { |
1227 | sub common_sense { |
1235 | require "AnyEvent/constants.pl"; |
1228 | # from common:.sense 3.5 |
1236 | &AnyEvent::common_sense; |
1229 | local $^W; |
|
|
1230 | ${^WARNING_BITS} ^= ${^WARNING_BITS} ^ "\x3c\x3f\x33\x00\x0f\xf0\x0f\xc0\xf0\xfc\x33\x00"; |
|
|
1231 | # use strict vars subs - NO UTF-8, as Util.pm doesn't like this atm. (uts46data.pl) |
|
|
1232 | $^H |= 0x00000600; |
|
|
1233 | } |
1237 | } |
1234 | |
|
|
1235 | BEGIN { AnyEvent::common_sense } |
|
|
1236 | |
1238 | |
1237 | use Carp (); |
1239 | use Carp (); |
1238 | |
1240 | |
1239 | our $VERSION = '6.14'; |
1241 | our $VERSION = '7.05'; |
1240 | our $MODEL; |
1242 | our $MODEL; |
1241 | our @ISA; |
1243 | our @ISA; |
1242 | our @REGISTRY; |
1244 | our @REGISTRY; |
1243 | our $VERBOSE; |
1245 | our $VERBOSE; |
1244 | our %PROTOCOL; # (ipv4|ipv6) => (1|2), higher numbers are preferred |
1246 | our %PROTOCOL; # (ipv4|ipv6) => (1|2), higher numbers are preferred |
1245 | our $MAX_SIGNAL_LATENCY = $ENV{PERL_ANYEVENT_MAX_SIGNAL_LATENCY} || 10; # executes after the BEGIN block below (tainting!) |
1247 | our $MAX_SIGNAL_LATENCY = $ENV{PERL_ANYEVENT_MAX_SIGNAL_LATENCY} || 10; # executes after the BEGIN block below (tainting!) |
1246 | |
1248 | |
1247 | BEGIN { |
1249 | BEGIN { |
1248 | require "AnyEvent/constants.pl"; |
|
|
1249 | |
|
|
1250 | eval "sub TAINT (){" . (${^TAINT}*1) . "}"; |
1250 | eval "sub TAINT (){" . (${^TAINT}*1) . "}"; |
1251 | |
1251 | |
1252 | delete @ENV{grep /^PERL_ANYEVENT_/, keys %ENV} |
1252 | delete @ENV{grep /^PERL_ANYEVENT_/, keys %ENV} |
1253 | if ${^TAINT}; |
1253 | if ${^TAINT}; |
1254 | |
1254 | |
… | |
… | |
2921 | This module is part of perl since release 5.008. It will be used when the |
2921 | This module is part of perl since release 5.008. It will be used when the |
2922 | chosen event library does not come with a timing source of its own. The |
2922 | chosen event library does not come with a timing source of its own. The |
2923 | pure-perl event loop (L<AnyEvent::Loop>) will additionally load it to |
2923 | pure-perl event loop (L<AnyEvent::Loop>) will additionally load it to |
2924 | try to use a monotonic clock for timing stability. |
2924 | try to use a monotonic clock for timing stability. |
2925 | |
2925 | |
|
|
2926 | =item L<AnyEvent::AIO> (and L<IO::AIO>) |
|
|
2927 | |
|
|
2928 | The default implementation of L<AnyEvent::IO> is to do I/O synchronously, |
|
|
2929 | stopping programs while they access the disk, which is fine for a lot of |
|
|
2930 | programs. |
|
|
2931 | |
|
|
2932 | Installing AnyEvent::AIO (and its IO::AIO dependency) makes it switch to |
|
|
2933 | a true asynchronous implementation, so event processing can continue even |
|
|
2934 | while waiting for disk I/O. |
|
|
2935 | |
2926 | =back |
2936 | =back |
2927 | |
2937 | |
2928 | |
2938 | |
2929 | =head1 FORK |
2939 | =head1 FORK |
2930 | |
2940 | |
… | |
… | |
3026 | |
3036 | |
3027 | |
3037 | |
3028 | =head1 AUTHOR |
3038 | =head1 AUTHOR |
3029 | |
3039 | |
3030 | Marc Lehmann <schmorp@schmorp.de> |
3040 | Marc Lehmann <schmorp@schmorp.de> |
3031 | http://home.schmorp.de/ |
3041 | http://anyevent.schmorp.de |
3032 | |
3042 | |
3033 | =cut |
3043 | =cut |
3034 | |
3044 | |
3035 | 1 |
3045 | 1 |
3036 | |
3046 | |