… | |
… | |
6 | |
6 | |
7 | =head1 SYNOPSIS |
7 | =head1 SYNOPSIS |
8 | |
8 | |
9 | use AnyEvent; |
9 | use AnyEvent; |
10 | |
10 | |
11 | my $w = AnyEvent->io (fh => $fh, poll => "r|w", cb => sub { |
11 | my $w = AnyEvent->io (fh => $fh, poll => "r|w", cb => sub { ... }); |
12 | ... |
|
|
13 | }); |
|
|
14 | |
12 | |
15 | my $w = AnyEvent->timer (after => $seconds, cb => sub { |
13 | my $w = AnyEvent->timer (after => $seconds, cb => sub { ... }); |
|
|
14 | my $w = AnyEvent->timer (after => $seconds, interval => $seconds, cb => ... |
|
|
15 | |
|
|
16 | print AnyEvent->now; # prints current event loop time |
|
|
17 | print AnyEvent->time; # think Time::HiRes::time or simply CORE::time. |
|
|
18 | |
|
|
19 | my $w = AnyEvent->signal (signal => "TERM", cb => sub { ... }); |
|
|
20 | |
|
|
21 | my $w = AnyEvent->child (pid => $pid, cb => sub { |
|
|
22 | my ($pid, $status) = @_; |
16 | ... |
23 | ... |
17 | }); |
24 | }); |
18 | |
25 | |
19 | my $w = AnyEvent->condvar; # stores whether a condition was flagged |
26 | my $w = AnyEvent->condvar; # stores whether a condition was flagged |
20 | $w->send; # wake up current and all future recv's |
27 | $w->send; # wake up current and all future recv's |
21 | $w->recv; # enters "main loop" till $condvar gets ->send |
28 | $w->recv; # enters "main loop" till $condvar gets ->send |
|
|
29 | # use a condvar in callback mode: |
|
|
30 | $w->cb (sub { $_[0]->recv }); |
22 | |
31 | |
23 | =head1 INTRODUCTION/TUTORIAL |
32 | =head1 INTRODUCTION/TUTORIAL |
24 | |
33 | |
25 | This manpage is mainly a reference manual. If you are interested |
34 | This manpage is mainly a reference manual. If you are interested |
26 | in a tutorial or some gentle introduction, have a look at the |
35 | in a tutorial or some gentle introduction, have a look at the |
… | |
… | |
381 | The instrument to do that is called a "condition variable", so called |
390 | The instrument to do that is called a "condition variable", so called |
382 | because they represent a condition that must become true. |
391 | because they represent a condition that must become true. |
383 | |
392 | |
384 | Condition variables can be created by calling the C<< AnyEvent->condvar |
393 | Condition variables can be created by calling the C<< AnyEvent->condvar |
385 | >> method, usually without arguments. The only argument pair allowed is |
394 | >> method, usually without arguments. The only argument pair allowed is |
|
|
395 | |
386 | C<cb>, which specifies a callback to be called when the condition variable |
396 | C<cb>, which specifies a callback to be called when the condition variable |
387 | becomes true. |
397 | becomes true, with the condition variable as the first argument (but not |
|
|
398 | the results). |
388 | |
399 | |
389 | After creation, the condition variable is "false" until it becomes "true" |
400 | After creation, the condition variable is "false" until it becomes "true" |
390 | by calling the C<send> method (or calling the condition variable as if it |
401 | by calling the C<send> method (or calling the condition variable as if it |
391 | were a callback, read about the caveats in the description for the C<< |
402 | were a callback, read about the caveats in the description for the C<< |
392 | ->send >> method). |
403 | ->send >> method). |
… | |
… | |
448 | |
459 | |
449 | my $done = AnyEvent->condvar; |
460 | my $done = AnyEvent->condvar; |
450 | my $delay = AnyEvent->timer (after => 5, cb => $done); |
461 | my $delay = AnyEvent->timer (after => 5, cb => $done); |
451 | $done->recv; |
462 | $done->recv; |
452 | |
463 | |
|
|
464 | Example: Imagine an API that returns a condvar and doesn't support |
|
|
465 | callbacks. This is how you make a synchronous call, for example from |
|
|
466 | the main program: |
|
|
467 | |
|
|
468 | use AnyEvent::CouchDB; |
|
|
469 | |
|
|
470 | ... |
|
|
471 | |
|
|
472 | my @info = $couchdb->info->recv; |
|
|
473 | |
|
|
474 | And this is how you would just ste a callback to be called whenever the |
|
|
475 | results are available: |
|
|
476 | |
|
|
477 | $couchdb->info->cb (sub { |
|
|
478 | my @info = $_[0]->recv; |
|
|
479 | }); |
|
|
480 | |
453 | =head3 METHODS FOR PRODUCERS |
481 | =head3 METHODS FOR PRODUCERS |
454 | |
482 | |
455 | These methods should only be used by the producing side, i.e. the |
483 | These methods should only be used by the producing side, i.e. the |
456 | code/module that eventually sends the signal. Note that it is also |
484 | code/module that eventually sends the signal. Note that it is also |
457 | the producer side which creates the condvar in most cases, but it isn't |
485 | the producer side which creates the condvar in most cases, but it isn't |
… | |
… | |
590 | =item $bool = $cv->ready |
618 | =item $bool = $cv->ready |
591 | |
619 | |
592 | Returns true when the condition is "true", i.e. whether C<send> or |
620 | Returns true when the condition is "true", i.e. whether C<send> or |
593 | C<croak> have been called. |
621 | C<croak> have been called. |
594 | |
622 | |
595 | =item $cb = $cv->cb ([new callback]) |
623 | =item $cb = $cv->cb ($cb->($cv)) |
596 | |
624 | |
597 | This is a mutator function that returns the callback set and optionally |
625 | This is a mutator function that returns the callback set and optionally |
598 | replaces it before doing so. |
626 | replaces it before doing so. |
599 | |
627 | |
600 | The callback will be called when the condition becomes "true", i.e. when |
628 | The callback will be called when the condition becomes "true", i.e. when |
… | |
… | |
822 | =cut |
850 | =cut |
823 | |
851 | |
824 | package AnyEvent; |
852 | package AnyEvent; |
825 | |
853 | |
826 | no warnings; |
854 | no warnings; |
827 | use strict; |
855 | use strict qw(vars subs); |
828 | |
856 | |
829 | use Carp; |
857 | use Carp; |
830 | |
858 | |
831 | our $VERSION = 4.2; |
859 | our $VERSION = 4.233; |
832 | our $MODEL; |
860 | our $MODEL; |
833 | |
861 | |
834 | our $AUTOLOAD; |
862 | our $AUTOLOAD; |
835 | our @ISA; |
863 | our @ISA; |
836 | |
864 | |
… | |
… | |
988 | |
1016 | |
989 | package AnyEvent::Base; |
1017 | package AnyEvent::Base; |
990 | |
1018 | |
991 | # default implementation for now and time |
1019 | # default implementation for now and time |
992 | |
1020 | |
993 | use Time::HiRes (); |
1021 | BEGIN { |
|
|
1022 | if (eval "use Time::HiRes (); time (); 1") { |
|
|
1023 | *_time = \&Time::HiRes::time; |
|
|
1024 | # if (eval "use POSIX (); (POSIX::times())... |
|
|
1025 | } else { |
|
|
1026 | *_time = \&CORE::time; # epic fail |
|
|
1027 | } |
|
|
1028 | } |
994 | |
1029 | |
995 | sub time { Time::HiRes::time } |
1030 | sub time { _time } |
996 | sub now { Time::HiRes::time } |
1031 | sub now { _time } |
997 | |
1032 | |
998 | # default implementation for ->condvar |
1033 | # default implementation for ->condvar |
999 | |
1034 | |
1000 | sub condvar { |
1035 | sub condvar { |
1001 | bless { @_ == 3 ? (_ae_cb => $_[2]) : () }, AnyEvent::CondVar:: |
1036 | bless { @_ == 3 ? (_ae_cb => $_[2]) : () }, AnyEvent::CondVar:: |
… | |
… | |
1140 | |
1175 | |
1141 | # undocumented/compatibility with pre-3.4 |
1176 | # undocumented/compatibility with pre-3.4 |
1142 | *broadcast = \&send; |
1177 | *broadcast = \&send; |
1143 | *wait = \&_wait; |
1178 | *wait = \&_wait; |
1144 | |
1179 | |
|
|
1180 | =head1 ERROR AND EXCEPTION HANDLING |
|
|
1181 | |
|
|
1182 | In general, AnyEvent does not do any error handling - it relies on the |
|
|
1183 | caller to do that if required. The L<AnyEvent::Strict> module (see also |
|
|
1184 | the C<PERL_ANYEVENT_STRICT> environment variable, below) provides strict |
|
|
1185 | checking of all AnyEvent methods, however, which is highly useful during |
|
|
1186 | development. |
|
|
1187 | |
|
|
1188 | As for exception handling (i.e. runtime errors and exceptions thrown while |
|
|
1189 | executing a callback), this is not only highly event-loop specific, but |
|
|
1190 | also not in any way wrapped by this module, as this is the job of the main |
|
|
1191 | program. |
|
|
1192 | |
|
|
1193 | The pure perl event loop simply re-throws the exception (usually |
|
|
1194 | within C<< condvar->recv >>), the L<Event> and L<EV> modules call C<< |
|
|
1195 | $Event/EV::DIED->() >>, L<Glib> uses C<< install_exception_handler >> and |
|
|
1196 | so on. |
|
|
1197 | |
|
|
1198 | =head1 ENVIRONMENT VARIABLES |
|
|
1199 | |
|
|
1200 | The following environment variables are used by this module or its |
|
|
1201 | submodules: |
|
|
1202 | |
|
|
1203 | =over 4 |
|
|
1204 | |
|
|
1205 | =item C<PERL_ANYEVENT_VERBOSE> |
|
|
1206 | |
|
|
1207 | By default, AnyEvent will be completely silent except in fatal |
|
|
1208 | conditions. You can set this environment variable to make AnyEvent more |
|
|
1209 | talkative. |
|
|
1210 | |
|
|
1211 | When set to C<1> or higher, causes AnyEvent to warn about unexpected |
|
|
1212 | conditions, such as not being able to load the event model specified by |
|
|
1213 | C<PERL_ANYEVENT_MODEL>. |
|
|
1214 | |
|
|
1215 | When set to C<2> or higher, cause AnyEvent to report to STDERR which event |
|
|
1216 | model it chooses. |
|
|
1217 | |
|
|
1218 | =item C<PERL_ANYEVENT_STRICT> |
|
|
1219 | |
|
|
1220 | AnyEvent does not do much argument checking by default, as thorough |
|
|
1221 | argument checking is very costly. Setting this variable to a true value |
|
|
1222 | will cause AnyEvent to load C<AnyEvent::Strict> and then to thoroughly |
|
|
1223 | check the arguments passed to most method calls. If it finds any problems |
|
|
1224 | it will croak. |
|
|
1225 | |
|
|
1226 | In other words, enables "strict" mode. |
|
|
1227 | |
|
|
1228 | Unlike C<use strict>, it is definitely recommended ot keep it off in |
|
|
1229 | production. Keeping C<PERL_ANYEVENT_STRICT=1> in your environment while |
|
|
1230 | developing programs can be very useful, however. |
|
|
1231 | |
|
|
1232 | =item C<PERL_ANYEVENT_MODEL> |
|
|
1233 | |
|
|
1234 | This can be used to specify the event model to be used by AnyEvent, before |
|
|
1235 | auto detection and -probing kicks in. It must be a string consisting |
|
|
1236 | entirely of ASCII letters. The string C<AnyEvent::Impl::> gets prepended |
|
|
1237 | and the resulting module name is loaded and if the load was successful, |
|
|
1238 | used as event model. If it fails to load AnyEvent will proceed with |
|
|
1239 | auto detection and -probing. |
|
|
1240 | |
|
|
1241 | This functionality might change in future versions. |
|
|
1242 | |
|
|
1243 | For example, to force the pure perl model (L<AnyEvent::Impl::Perl>) you |
|
|
1244 | could start your program like this: |
|
|
1245 | |
|
|
1246 | PERL_ANYEVENT_MODEL=Perl perl ... |
|
|
1247 | |
|
|
1248 | =item C<PERL_ANYEVENT_PROTOCOLS> |
|
|
1249 | |
|
|
1250 | Used by both L<AnyEvent::DNS> and L<AnyEvent::Socket> to determine preferences |
|
|
1251 | for IPv4 or IPv6. The default is unspecified (and might change, or be the result |
|
|
1252 | of auto probing). |
|
|
1253 | |
|
|
1254 | Must be set to a comma-separated list of protocols or address families, |
|
|
1255 | current supported: C<ipv4> and C<ipv6>. Only protocols mentioned will be |
|
|
1256 | used, and preference will be given to protocols mentioned earlier in the |
|
|
1257 | list. |
|
|
1258 | |
|
|
1259 | This variable can effectively be used for denial-of-service attacks |
|
|
1260 | against local programs (e.g. when setuid), although the impact is likely |
|
|
1261 | small, as the program has to handle connection errors already- |
|
|
1262 | |
|
|
1263 | Examples: C<PERL_ANYEVENT_PROTOCOLS=ipv4,ipv6> - prefer IPv4 over IPv6, |
|
|
1264 | but support both and try to use both. C<PERL_ANYEVENT_PROTOCOLS=ipv4> |
|
|
1265 | - only support IPv4, never try to resolve or contact IPv6 |
|
|
1266 | addresses. C<PERL_ANYEVENT_PROTOCOLS=ipv6,ipv4> support either IPv4 or |
|
|
1267 | IPv6, but prefer IPv6 over IPv4. |
|
|
1268 | |
|
|
1269 | =item C<PERL_ANYEVENT_EDNS0> |
|
|
1270 | |
|
|
1271 | Used by L<AnyEvent::DNS> to decide whether to use the EDNS0 extension |
|
|
1272 | for DNS. This extension is generally useful to reduce DNS traffic, but |
|
|
1273 | some (broken) firewalls drop such DNS packets, which is why it is off by |
|
|
1274 | default. |
|
|
1275 | |
|
|
1276 | Setting this variable to C<1> will cause L<AnyEvent::DNS> to announce |
|
|
1277 | EDNS0 in its DNS requests. |
|
|
1278 | |
|
|
1279 | =item C<PERL_ANYEVENT_MAX_FORKS> |
|
|
1280 | |
|
|
1281 | The maximum number of child processes that C<AnyEvent::Util::fork_call> |
|
|
1282 | will create in parallel. |
|
|
1283 | |
|
|
1284 | =back |
|
|
1285 | |
1145 | =head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE |
1286 | =head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE |
1146 | |
1287 | |
1147 | This is an advanced topic that you do not normally need to use AnyEvent in |
1288 | This is an advanced topic that you do not normally need to use AnyEvent in |
1148 | a module. This section is only of use to event loop authors who want to |
1289 | a module. This section is only of use to event loop authors who want to |
1149 | provide AnyEvent compatibility. |
1290 | provide AnyEvent compatibility. |
… | |
… | |
1182 | |
1323 | |
1183 | I<rxvt-unicode> also cheats a bit by not providing blocking access to |
1324 | I<rxvt-unicode> also cheats a bit by not providing blocking access to |
1184 | condition variables: code blocking while waiting for a condition will |
1325 | condition variables: code blocking while waiting for a condition will |
1185 | C<die>. This still works with most modules/usages, and blocking calls must |
1326 | C<die>. This still works with most modules/usages, and blocking calls must |
1186 | not be done in an interactive application, so it makes sense. |
1327 | not be done in an interactive application, so it makes sense. |
1187 | |
|
|
1188 | =head1 ENVIRONMENT VARIABLES |
|
|
1189 | |
|
|
1190 | The following environment variables are used by this module: |
|
|
1191 | |
|
|
1192 | =over 4 |
|
|
1193 | |
|
|
1194 | =item C<PERL_ANYEVENT_VERBOSE> |
|
|
1195 | |
|
|
1196 | By default, AnyEvent will be completely silent except in fatal |
|
|
1197 | conditions. You can set this environment variable to make AnyEvent more |
|
|
1198 | talkative. |
|
|
1199 | |
|
|
1200 | When set to C<1> or higher, causes AnyEvent to warn about unexpected |
|
|
1201 | conditions, such as not being able to load the event model specified by |
|
|
1202 | C<PERL_ANYEVENT_MODEL>. |
|
|
1203 | |
|
|
1204 | When set to C<2> or higher, cause AnyEvent to report to STDERR which event |
|
|
1205 | model it chooses. |
|
|
1206 | |
|
|
1207 | =item C<PERL_ANYEVENT_STRICT> |
|
|
1208 | |
|
|
1209 | AnyEvent does not do much argument checking by default, as thorough |
|
|
1210 | argument checking is very costly. Setting this variable to a true value |
|
|
1211 | will cause AnyEvent to load C<AnyEvent::Strict> and then to thoroughly |
|
|
1212 | check the arguments passed to most method calls. If it finds any problems |
|
|
1213 | it will croak. |
|
|
1214 | |
|
|
1215 | In other words, enables "strict" mode. |
|
|
1216 | |
|
|
1217 | Unlike C<use strict> it is definitely recommended ot keep it off in |
|
|
1218 | production. |
|
|
1219 | |
|
|
1220 | =item C<PERL_ANYEVENT_MODEL> |
|
|
1221 | |
|
|
1222 | This can be used to specify the event model to be used by AnyEvent, before |
|
|
1223 | auto detection and -probing kicks in. It must be a string consisting |
|
|
1224 | entirely of ASCII letters. The string C<AnyEvent::Impl::> gets prepended |
|
|
1225 | and the resulting module name is loaded and if the load was successful, |
|
|
1226 | used as event model. If it fails to load AnyEvent will proceed with |
|
|
1227 | auto detection and -probing. |
|
|
1228 | |
|
|
1229 | This functionality might change in future versions. |
|
|
1230 | |
|
|
1231 | For example, to force the pure perl model (L<AnyEvent::Impl::Perl>) you |
|
|
1232 | could start your program like this: |
|
|
1233 | |
|
|
1234 | PERL_ANYEVENT_MODEL=Perl perl ... |
|
|
1235 | |
|
|
1236 | =item C<PERL_ANYEVENT_PROTOCOLS> |
|
|
1237 | |
|
|
1238 | Used by both L<AnyEvent::DNS> and L<AnyEvent::Socket> to determine preferences |
|
|
1239 | for IPv4 or IPv6. The default is unspecified (and might change, or be the result |
|
|
1240 | of auto probing). |
|
|
1241 | |
|
|
1242 | Must be set to a comma-separated list of protocols or address families, |
|
|
1243 | current supported: C<ipv4> and C<ipv6>. Only protocols mentioned will be |
|
|
1244 | used, and preference will be given to protocols mentioned earlier in the |
|
|
1245 | list. |
|
|
1246 | |
|
|
1247 | This variable can effectively be used for denial-of-service attacks |
|
|
1248 | against local programs (e.g. when setuid), although the impact is likely |
|
|
1249 | small, as the program has to handle connection errors already- |
|
|
1250 | |
|
|
1251 | Examples: C<PERL_ANYEVENT_PROTOCOLS=ipv4,ipv6> - prefer IPv4 over IPv6, |
|
|
1252 | but support both and try to use both. C<PERL_ANYEVENT_PROTOCOLS=ipv4> |
|
|
1253 | - only support IPv4, never try to resolve or contact IPv6 |
|
|
1254 | addresses. C<PERL_ANYEVENT_PROTOCOLS=ipv6,ipv4> support either IPv4 or |
|
|
1255 | IPv6, but prefer IPv6 over IPv4. |
|
|
1256 | |
|
|
1257 | =item C<PERL_ANYEVENT_EDNS0> |
|
|
1258 | |
|
|
1259 | Used by L<AnyEvent::DNS> to decide whether to use the EDNS0 extension |
|
|
1260 | for DNS. This extension is generally useful to reduce DNS traffic, but |
|
|
1261 | some (broken) firewalls drop such DNS packets, which is why it is off by |
|
|
1262 | default. |
|
|
1263 | |
|
|
1264 | Setting this variable to C<1> will cause L<AnyEvent::DNS> to announce |
|
|
1265 | EDNS0 in its DNS requests. |
|
|
1266 | |
|
|
1267 | =item C<PERL_ANYEVENT_MAX_FORKS> |
|
|
1268 | |
|
|
1269 | The maximum number of child processes that C<AnyEvent::Util::fork_call> |
|
|
1270 | will create in parallel. |
|
|
1271 | |
|
|
1272 | =back |
|
|
1273 | |
1328 | |
1274 | =head1 EXAMPLE PROGRAM |
1329 | =head1 EXAMPLE PROGRAM |
1275 | |
1330 | |
1276 | The following program uses an I/O watcher to read data from STDIN, a timer |
1331 | The following program uses an I/O watcher to read data from STDIN, a timer |
1277 | to display a message once per second, and a condition variable to quit the |
1332 | to display a message once per second, and a condition variable to quit the |