1 | NAME |
1 | NAME |
2 | AnyEvent - provide framework for multiple event loops |
2 | AnyEvent - provide framework for multiple event loops |
3 | |
3 | |
4 | EV, Event, Glib, Tk, Perl, Event::Lib, Qt, POE - various supported event |
4 | EV, Event, Glib, Tk, Perl, Event::Lib, Qt and POE are various supported |
5 | loops |
5 | event loops. |
6 | |
6 | |
7 | SYNOPSIS |
7 | SYNOPSIS |
8 | use AnyEvent; |
8 | use AnyEvent; |
9 | |
9 | |
10 | # file descriptor readable |
10 | # file descriptor readable |
… | |
… | |
392 | you "fork" the child (alternatively, you can call "AnyEvent::detect"). |
392 | you "fork" the child (alternatively, you can call "AnyEvent::detect"). |
393 | |
393 | |
394 | Example: fork a process and wait for it |
394 | Example: fork a process and wait for it |
395 | |
395 | |
396 | my $done = AnyEvent->condvar; |
396 | my $done = AnyEvent->condvar; |
397 | |
397 | |
398 | my $pid = fork or exit 5; |
398 | my $pid = fork or exit 5; |
399 | |
399 | |
400 | my $w = AnyEvent->child ( |
400 | my $w = AnyEvent->child ( |
401 | pid => $pid, |
401 | pid => $pid, |
402 | cb => sub { |
402 | cb => sub { |
403 | my ($pid, $status) = @_; |
403 | my ($pid, $status) = @_; |
404 | warn "pid $pid exited with status $status"; |
404 | warn "pid $pid exited with status $status"; |
405 | $done->send; |
405 | $done->send; |
406 | }, |
406 | }, |
407 | ); |
407 | ); |
408 | |
408 | |
409 | # do something else, then wait for process exit |
409 | # do something else, then wait for process exit |
410 | $done->recv; |
410 | $done->recv; |
411 | |
411 | |
412 | IDLE WATCHERS |
412 | IDLE WATCHERS |
413 | Sometimes there is a need to do something, but it is not so important to |
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 |
414 | do it instantly, but only when there is nothing better to do. This |
… | |
… | |
883 | "condvar->recv"), the Event and EV modules call "$Event/EV::DIED->()", |
883 | "condvar->recv"), the Event and EV modules call "$Event/EV::DIED->()", |
884 | Glib uses "install_exception_handler" and so on. |
884 | Glib uses "install_exception_handler" and so on. |
885 | |
885 | |
886 | ENVIRONMENT VARIABLES |
886 | ENVIRONMENT VARIABLES |
887 | The following environment variables are used by this module or its |
887 | The following environment variables are used by this module or its |
888 | submodules: |
888 | submodules. |
|
|
889 | |
|
|
890 | Note that AnyEvent will remove *all* environment variables starting with |
|
|
891 | "PERL_ANYEVENT_" from %ENV when it is loaded while taint mode is |
|
|
892 | enabled. |
889 | |
893 | |
890 | "PERL_ANYEVENT_VERBOSE" |
894 | "PERL_ANYEVENT_VERBOSE" |
891 | By default, AnyEvent will be completely silent except in fatal |
895 | By default, AnyEvent will be completely silent except in fatal |
892 | conditions. You can set this environment variable to make AnyEvent |
896 | conditions. You can set this environment variable to make AnyEvent |
893 | more talkative. |
897 | more talkative. |
… | |
… | |
1398 | |
1402 | |
1399 | Summary |
1403 | Summary |
1400 | * C-based event loops perform very well with small number of watchers, |
1404 | * C-based event loops perform very well with small number of watchers, |
1401 | as the management overhead dominates. |
1405 | as the management overhead dominates. |
1402 | |
1406 | |
|
|
1407 | THE IO::Lambda BENCHMARK |
|
|
1408 | Recently I was told about the benchmark in the IO::Lambda manpage, which |
|
|
1409 | could be misinterpreted to make AnyEvent look bad. In fact, the |
|
|
1410 | benchmark simply compares IO::Lambda with POE, and IO::Lambda looks |
|
|
1411 | better (which shouldn't come as a surprise to anybody). As such, the |
|
|
1412 | benchmark is fine, and shows that the AnyEvent backend from IO::Lambda |
|
|
1413 | isn't very optimal. But how would AnyEvent compare when used without the |
|
|
1414 | extra baggage? To explore this, I wrote the equivalent benchmark for |
|
|
1415 | AnyEvent. |
|
|
1416 | |
|
|
1417 | The benchmark itself creates an echo-server, and then, for 500 times, |
|
|
1418 | connects to the echo server, sends a line, waits for the reply, and then |
|
|
1419 | creates the next connection. This is a rather bad benchmark, as it |
|
|
1420 | doesn't test the efficiency of the framework, but it is a benchmark |
|
|
1421 | nevertheless. |
|
|
1422 | |
|
|
1423 | name runtime |
|
|
1424 | Lambda/select 0.330 sec |
|
|
1425 | + optimized 0.122 sec |
|
|
1426 | Lambda/AnyEvent 0.327 sec |
|
|
1427 | + optimized 0.138 sec |
|
|
1428 | Raw sockets/select 0.077 sec |
|
|
1429 | POE/select, components 0.662 sec |
|
|
1430 | POE/select, raw sockets 0.226 sec |
|
|
1431 | POE/select, optimized 0.404 sec |
|
|
1432 | |
|
|
1433 | AnyEvent/select/nb 0.085 sec |
|
|
1434 | AnyEvent/EV/nb 0.068 sec |
|
|
1435 | +state machine 0.134 sec |
|
|
1436 | |
|
|
1437 | The benchmark is also a bit unfair (my fault) - the IO::Lambda |
|
|
1438 | benchmarks actually make blocking connects and use 100% blocking I/O, |
|
|
1439 | defeating the purpose of an event-based solution. All of the newly |
|
|
1440 | written AnyEvent benchmarks use 100% non-blocking connects (using |
|
|
1441 | AnyEvent::Socket::tcp_connect and the asynchronous pure perl DNS |
|
|
1442 | resolver), so AnyEvent is at a disadvantage here as non-blocking |
|
|
1443 | connects generally require a lot more bookkeeping and event handling |
|
|
1444 | than blocking connects (which involve a single syscall only). |
|
|
1445 | |
|
|
1446 | The last AnyEvent benchmark additionally uses AnyEvent::Handle, which |
|
|
1447 | offers similar expressive power as POE and IO::Lambda (using |
|
|
1448 | conventional Perl syntax), which means both the echo server and the |
|
|
1449 | client are 100% non-blocking w.r.t. I/O, further placing it at a |
|
|
1450 | disadvantage. |
|
|
1451 | |
|
|
1452 | As you can see, AnyEvent + EV even beats the hand-optimised "raw sockets |
|
|
1453 | benchmark", while AnyEvent + its pure perl backend easily beats |
|
|
1454 | IO::Lambda and POE. |
|
|
1455 | |
|
|
1456 | And even the 100% non-blocking version written using the high-level (and |
|
|
1457 | slow :) AnyEvent::Handle abstraction beats both POE and IO::Lambda, even |
|
|
1458 | thought it does all of DNS, tcp-connect and socket I/O in a non-blocking |
|
|
1459 | way. |
|
|
1460 | |
|
|
1461 | The two AnyEvent benchmarks can be found as eg/ae0.pl and eg/ae2.pl in |
|
|
1462 | the AnyEvent distribution, the remaining benchmarks are part of the |
|
|
1463 | IO::lambda distribution and were used without any changes. |
|
|
1464 | |
1403 | SIGNALS |
1465 | SIGNALS |
1404 | AnyEvent currently installs handlers for these signals: |
1466 | AnyEvent currently installs handlers for these signals: |
1405 | |
1467 | |
1406 | SIGCHLD |
1468 | SIGCHLD |
1407 | A handler for "SIGCHLD" is installed by AnyEvent's child watcher |
1469 | A handler for "SIGCHLD" is installed by AnyEvent's child watcher |
… | |
… | |
1442 | |
1504 | |
1443 | You can make AnyEvent completely ignore this variable by deleting it |
1505 | You can make AnyEvent completely ignore this variable by deleting it |
1444 | before the first watcher gets created, e.g. with a "BEGIN" block: |
1506 | before the first watcher gets created, e.g. with a "BEGIN" block: |
1445 | |
1507 | |
1446 | BEGIN { delete $ENV{PERL_ANYEVENT_MODEL} } |
1508 | BEGIN { delete $ENV{PERL_ANYEVENT_MODEL} } |
1447 | |
1509 | |
1448 | use AnyEvent; |
1510 | use AnyEvent; |
1449 | |
1511 | |
1450 | Similar considerations apply to $ENV{PERL_ANYEVENT_VERBOSE}, as that can |
1512 | Similar considerations apply to $ENV{PERL_ANYEVENT_VERBOSE}, as that can |
1451 | be used to probe what backend is used and gain other information (which |
1513 | be used to probe what backend is used and gain other information (which |
1452 | is probably even less useful to an attacker than PERL_ANYEVENT_MODEL), |
1514 | is probably even less useful to an attacker than PERL_ANYEVENT_MODEL), |
1453 | and $ENV{PERL_ANYEGENT_STRICT}. |
1515 | and $ENV{PERL_ANYEVENT_STRICT}. |
1454 | |
1516 | |
1455 | BUGS |
1517 | BUGS |
1456 | Perl 5.8 has numerous memleaks that sometimes hit this module and are |
1518 | Perl 5.8 has numerous memleaks that sometimes hit this module and are |
1457 | hard to work around. If you suffer from memleaks, first upgrade to Perl |
1519 | hard to work around. If you suffer from memleaks, first upgrade to Perl |
1458 | 5.10 and check wether the leaks still show up. (Perl 5.10.0 has other |
1520 | 5.10 and check wether the leaks still show up. (Perl 5.10.0 has other |