ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent.pm
(Generate patch)

Comparing AnyEvent/lib/AnyEvent.pm (file contents):
Revision 1.99 by root, Sun Apr 27 17:09:33 2008 UTC vs.
Revision 1.104 by root, Wed Apr 30 11:40:22 2008 UTC

65technically possible. 65technically possible.
66 66
67Of course, if you want lots of policy (this can arguably be somewhat 67Of course, if you want lots of policy (this can arguably be somewhat
68useful) and you want to force your users to use the one and only event 68useful) and you want to force your users to use the one and only event
69model, you should I<not> use this module. 69model, you should I<not> use this module.
70
71#TODO#
72
73Net::IRC3
74AnyEvent::HTTPD
75AnyEvent::DNS
76IO::AnyEvent
77Net::FPing
78Net::XMPP2
79Coro
80
81AnyEvent::IRC
82AnyEvent::HTTPD
83AnyEvent::DNS
84AnyEvent::Handle
85AnyEvent::Socket
86AnyEvent::FPing
87AnyEvent::XMPP
88AnyEvent::SNMP
89Coro
90 70
91=head1 DESCRIPTION 71=head1 DESCRIPTION
92 72
93L<AnyEvent> provides an identical interface to multiple event loops. This 73L<AnyEvent> provides an identical interface to multiple event loops. This
94allows module authors to utilise an event loop without forcing module 74allows module authors to utilise an event loop without forcing module
410 390
411 AnyEvent::Impl::CoroEV based on Coro::EV, best choice. 391 AnyEvent::Impl::CoroEV based on Coro::EV, best choice.
412 AnyEvent::Impl::CoroEvent based on Coro::Event, second best choice. 392 AnyEvent::Impl::CoroEvent based on Coro::Event, second best choice.
413 AnyEvent::Impl::EV based on EV (an interface to libev, best choice). 393 AnyEvent::Impl::EV based on EV (an interface to libev, best choice).
414 AnyEvent::Impl::Event based on Event, second best choice. 394 AnyEvent::Impl::Event based on Event, second best choice.
395 AnyEvent::Impl::Perl pure-perl implementation, fast and portable.
415 AnyEvent::Impl::Glib based on Glib, third-best choice. 396 AnyEvent::Impl::Glib based on Glib, third-best choice.
416 AnyEvent::Impl::Perl pure-perl implementation, inefficient but portable.
417 AnyEvent::Impl::Tk based on Tk, very bad choice. 397 AnyEvent::Impl::Tk based on Tk, very bad choice.
418 AnyEvent::Impl::Qt based on Qt, cannot be autoprobed (see its docs). 398 AnyEvent::Impl::Qt based on Qt, cannot be autoprobed (see its docs).
419 AnyEvent::Impl::EventLib based on Event::Lib, leaks memory and worse. 399 AnyEvent::Impl::EventLib based on Event::Lib, leaks memory and worse.
420 AnyEvent::Impl::POE based on POE, not generic enough for full support. 400 AnyEvent::Impl::POE based on POE, not generic enough for full support.
421 401
477 457
478You can chose to use a rather inefficient pure-perl implementation by 458You can chose to use a rather inefficient pure-perl implementation by
479loading the C<AnyEvent::Impl::Perl> module, which gives you similar 459loading the C<AnyEvent::Impl::Perl> module, which gives you similar
480behaviour everywhere, but letting AnyEvent chose is generally better. 460behaviour everywhere, but letting AnyEvent chose is generally better.
481 461
462=head1 OTHER MODULES
463
464The following is a non-exhaustive list of additional modules that use
465AnyEvent and can therefore be mixed easily with other AnyEvent modules
466in the same program. Some of the modules come with AnyEvent, some are
467available via CPAN.
468
469=over 4
470
471=item L<AnyEvent::Util>
472
473Contains various utility functions that replace often-used but blocking
474functions such as C<inet_aton> by event-/callback-based versions.
475
476=item L<AnyEvent::Handle>
477
478Provide read and write buffers and manages watchers for reads and writes.
479
480=item L<AnyEvent::Socket>
481
482Provides a means to do non-blocking connects, accepts etc.
483
484=item L<AnyEvent::HTTPD>
485
486Provides a simple web application server framework.
487
488=item L<AnyEvent::DNS>
489
490Provides asynchronous DNS resolver capabilities, beyond what
491L<AnyEvent::Util> offers.
492
493=item L<AnyEvent::FastPing>
494
495The fastest ping in the west.
496
497=item L<Net::IRC3>
498
499AnyEvent based IRC client module family.
500
501=item L<Net::XMPP2>
502
503AnyEvent based XMPP (Jabber protocol) module family.
504
505=item L<Net::FCP>
506
507AnyEvent-based implementation of the Freenet Client Protocol, birthplace
508of AnyEvent.
509
510=item L<Event::ExecFlow>
511
512High level API for event-based execution flow control.
513
514=item L<Coro>
515
516Has special support for AnyEvent.
517
518=item L<IO::Lambda>
519
520The lambda approach to I/O - don't ask, look there. Can use AnyEvent.
521
522=item L<IO::AIO>
523
524Truly asynchronous I/O, should be in the toolbox of every event
525programmer. Can be trivially made to use AnyEvent.
526
527=item L<BDB>
528
529Truly asynchronous Berkeley DB access. Can be trivially made to use
530AnyEvent.
531
532=back
533
482=cut 534=cut
483 535
484package AnyEvent; 536package AnyEvent;
485 537
486no warnings; 538no warnings;
501my @models = ( 553my @models = (
502 [Coro::EV:: => AnyEvent::Impl::CoroEV::], 554 [Coro::EV:: => AnyEvent::Impl::CoroEV::],
503 [Coro::Event:: => AnyEvent::Impl::CoroEvent::], 555 [Coro::Event:: => AnyEvent::Impl::CoroEvent::],
504 [EV:: => AnyEvent::Impl::EV::], 556 [EV:: => AnyEvent::Impl::EV::],
505 [Event:: => AnyEvent::Impl::Event::], 557 [Event:: => AnyEvent::Impl::Event::],
506 [Glib:: => AnyEvent::Impl::Glib::],
507 [Tk:: => AnyEvent::Impl::Tk::], 558 [Tk:: => AnyEvent::Impl::Tk::],
508 [Wx:: => AnyEvent::Impl::POE::], 559 [Wx:: => AnyEvent::Impl::POE::],
509 [Prima:: => AnyEvent::Impl::POE::], 560 [Prima:: => AnyEvent::Impl::POE::],
510 [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], 561 [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::],
511 # everything below here will not be autoprobed as the pureperl backend should work everywhere 562 # everything below here will not be autoprobed as the pureperl backend should work everywhere
563 [Glib:: => AnyEvent::Impl::Glib::],
512 [Event::Lib:: => AnyEvent::Impl::EventLib::], # too buggy 564 [Event::Lib:: => AnyEvent::Impl::EventLib::], # too buggy
513 [Qt:: => AnyEvent::Impl::Qt::], # requires special main program 565 [Qt:: => AnyEvent::Impl::Qt::], # requires special main program
514 [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza 566 [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza
515); 567);
516 568
1019file descriptor is dup()ed for each watcher. This shows that the dup() 1071file descriptor is dup()ed for each watcher. This shows that the dup()
1020employed by some adaptors is not a big performance issue (it does incur a 1072employed by some adaptors is not a big performance issue (it does incur a
1021hidden memory cost inside the kernel which is not reflected in the figures 1073hidden memory cost inside the kernel which is not reflected in the figures
1022above). 1074above).
1023 1075
1024C<POE>, regardless of underlying event loop (whether using its pure 1076C<POE>, regardless of underlying event loop (whether using its pure perl
1025perl select-based backend or the Event module, the POE-EV backend 1077select-based backend or the Event module, the POE-EV backend couldn't
1026couldn't be tested because it wasn't working) shows abysmal performance 1078be tested because it wasn't working) shows abysmal performance and
1027and memory usage: Watchers use almost 30 times as much memory as 1079memory usage with AnyEvent: Watchers use almost 30 times as much memory
1028EV watchers, and 10 times as much memory as Event (the high memory 1080as EV watchers, and 10 times as much memory as Event (the high memory
1029requirements are caused by requiring a session for each watcher). Watcher 1081requirements are caused by requiring a session for each watcher). Watcher
1030invocation speed is almost 900 times slower than with AnyEvent's pure perl 1082invocation speed is almost 900 times slower than with AnyEvent's pure perl
1083implementation.
1084
1031implementation. The design of the POE adaptor class in AnyEvent can not 1085The design of the POE adaptor class in AnyEvent can not really account
1032really account for this, as session creation overhead is small compared 1086for the performance issues, though, as session creation overhead is
1033to execution of the state machine, which is coded pretty optimally within 1087small compared to execution of the state machine, which is coded pretty
1034L<AnyEvent::Impl::POE>. POE simply seems to be abysmally slow. 1088optimally within L<AnyEvent::Impl::POE> (and while everybody agrees that
1089using multiple sessions is not a good approach, especially regarding
1090memory usage, even the author of POE could not come up with a faster
1091design).
1035 1092
1036=head3 Summary 1093=head3 Summary
1037 1094
1038=over 4 1095=over 4
1039 1096
1118 1175
1119=head3 Summary 1176=head3 Summary
1120 1177
1121=over 4 1178=over 4
1122 1179
1123=item * The pure perl implementation performs extremely well, considering 1180=item * The pure perl implementation performs extremely well.
1124that it uses select.
1125 1181
1126=item * Avoid Glib or POE in large projects where performance matters. 1182=item * Avoid Glib or POE in large projects where performance matters.
1127 1183
1128=back 1184=back
1129 1185
1158speed most when you have lots of watchers, not when you only have a few of 1214speed most when you have lots of watchers, not when you only have a few of
1159them). 1215them).
1160 1216
1161EV is again fastest. 1217EV is again fastest.
1162 1218
1163The C-based event loops Event and Glib come in second this time, as the 1219Perl again comes second. It is noticably faster than the C-based event
1164overhead of running an iteration is much smaller in C than in Perl (little 1220loops Event and Glib, although the difference is too small to really
1165code to execute in the inner loop, and perl's function calling overhead is 1221matter.
1166high, and updating all the data structures is costly).
1167
1168The pure perl event loop is much slower, but still competitive.
1169 1222
1170POE also performs much better in this case, but is is still far behind the 1223POE also performs much better in this case, but is is still far behind the
1171others. 1224others.
1172 1225
1173=head3 Summary 1226=head3 Summary
1181 1234
1182 1235
1183=head1 FORK 1236=head1 FORK
1184 1237
1185Most event libraries are not fork-safe. The ones who are usually are 1238Most event libraries are not fork-safe. The ones who are usually are
1186because they are so inefficient. Only L<EV> is fully fork-aware. 1239because they rely on inefficient but fork-safe C<select> or C<poll>
1240calls. Only L<EV> is fully fork-aware.
1187 1241
1188If you have to fork, you must either do so I<before> creating your first 1242If you have to fork, you must either do so I<before> creating your first
1189watcher OR you must not use AnyEvent at all in the child. 1243watcher OR you must not use AnyEvent at all in the child.
1190 1244
1191 1245

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines