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

Comparing AnyEvent/README (file contents):
Revision 1.21 by root, Sat May 17 21:34:15 2008 UTC vs.
Revision 1.24 by root, Thu May 29 03:46:04 2008 UTC

1NAME 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, POE - various supported event
5 loops 5 loops
6 6
46 that isn't itself. What's worse, all the potential users of your module 46 that isn't itself. What's worse, all the potential users of your module
47 are *also* forced to use the same event loop you use. 47 are *also* forced to use the same event loop you use.
48 48
49 AnyEvent is different: AnyEvent + POE works fine. AnyEvent + Glib works 49 AnyEvent is different: AnyEvent + POE works fine. AnyEvent + Glib works
50 fine. AnyEvent + Tk works fine etc. etc. but none of these work together 50 fine. AnyEvent + Tk works fine etc. etc. but none of these work together
51 with the rest: POE + IO::Async? no go. Tk + Event? no go. Again: if your 51 with the rest: POE + IO::Async? No go. Tk + Event? No go. Again: if your
52 module uses one of those, every user of your module has to use it, too. 52 module uses one of those, every user of your module has to use it, too.
53 But if your module uses AnyEvent, it works transparently with all event 53 But if your module uses AnyEvent, it works transparently with all event
54 models it supports (including stuff like POE and IO::Async, as long as 54 models it supports (including stuff like POE and IO::Async, as long as
55 those use one of the supported event loops. It is trivial to add new 55 those use one of the supported event loops. It is trivial to add new
56 event loops to AnyEvent, too, so it is future-proof). 56 event loops to AnyEvent, too, so it is future-proof).
57 57
58 In addition to being free of having to use *the one and only true event 58 In addition to being free of having to use *the one and only true event
59 model*, AnyEvent also is free of bloat and policy: with POE or similar 59 model*, AnyEvent also is free of bloat and policy: with POE or similar
60 modules, you get an enourmous amount of code and strict rules you have 60 modules, you get an enormous amount of code and strict rules you have to
61 to follow. AnyEvent, on the other hand, is lean and up to the point, by 61 follow. AnyEvent, on the other hand, is lean and up to the point, by
62 only offering the functionality that is necessary, in as thin as a 62 only offering the functionality that is necessary, in as thin as a
63 wrapper as technically possible. 63 wrapper as technically possible.
64 64
65 Of course, AnyEvent comes with a big (and fully optional!) toolbox of
66 useful functionality, such as an asynchronous DNS resolver, 100%
67 non-blocking connects (even with TLS/SSL, IPv6 and on broken platforms
68 such as Windows) and lots of real-world knowledge and workarounds for
69 platform bugs and differences.
70
65 Of course, if you want lots of policy (this can arguably be somewhat 71 Now, if you *do want* lots of policy (this can arguably be somewhat
66 useful) and you want to force your users to use the one and only event 72 useful) and you want to force your users to use the one and only event
67 model, you should *not* use this module. 73 model, you should *not* use this module.
68 74
69DESCRIPTION 75DESCRIPTION
70 AnyEvent provides an identical interface to multiple event loops. This 76 AnyEvent provides an identical interface to multiple event loops. This
99 starts using it, all bets are off. Maybe you should tell their authors 105 starts using it, all bets are off. Maybe you should tell their authors
100 to use AnyEvent so their modules work together with others seamlessly... 106 to use AnyEvent so their modules work together with others seamlessly...
101 107
102 The pure-perl implementation of AnyEvent is called 108 The pure-perl implementation of AnyEvent is called
103 "AnyEvent::Impl::Perl". Like other event modules you can load it 109 "AnyEvent::Impl::Perl". Like other event modules you can load it
104 explicitly. 110 explicitly and enjoy the high availability of that event loop :)
105 111
106WATCHERS 112WATCHERS
107 AnyEvent has the central concept of a *watcher*, which is an object that 113 AnyEvent has the central concept of a *watcher*, which is an object that
108 stores relevant data for each kind of event you are waiting for, such as 114 stores relevant data for each kind of event you are waiting for, such as
109 the callback to call, the filehandle to watch, etc. 115 the callback to call, the file handle to watch, etc.
110 116
111 These watchers are normal Perl objects with normal Perl lifetime. After 117 These watchers are normal Perl objects with normal Perl lifetime. After
112 creating a watcher it will immediately "watch" for events and invoke the 118 creating a watcher it will immediately "watch" for events and invoke the
113 callback when the event occurs (of course, only when the event model is 119 callback when the event occurs (of course, only when the event model is
114 in control). 120 in control).
220 on wallclock time) timers. 226 on wallclock time) timers.
221 227
222 AnyEvent always prefers relative timers, if available, matching the 228 AnyEvent always prefers relative timers, if available, matching the
223 AnyEvent API. 229 AnyEvent API.
224 230
231 AnyEvent has two additional methods that return the "current time":
232
233 AnyEvent->time
234 This returns the "current wallclock time" as a fractional number of
235 seconds since the Epoch (the same thing as "time" or
236 "Time::HiRes::time" return, and the result is guaranteed to be
237 compatible with those).
238
239 It progresses independently of any event loop processing, i.e. each
240 call will check the system clock, which usually gets updated
241 frequently.
242
243 AnyEvent->now
244 This also returns the "current wallclock time", but unlike "time",
245 above, this value might change only once per event loop iteration,
246 depending on the event loop (most return the same time as "time",
247 above). This is the time that AnyEvent's timers get scheduled
248 against.
249
250 *In almost all cases (in all cases if you don't care), this is the
251 function to call when you want to know the current time.*
252
253 This function is also often faster then "AnyEvent->time", and thus
254 the preferred method if you want some timestamp (for example,
255 AnyEvent::Handle uses this to update it's activity timeouts).
256
257 The rest of this section is only of relevance if you try to be very
258 exact with your timing, you can skip it without bad conscience.
259
260 For a practical example of when these times differ, consider
261 Event::Lib and EV and the following set-up:
262
263 The event loop is running and has just invoked one of your callback
264 at time=500 (assume no other callbacks delay processing). In your
265 callback, you wait a second by executing "sleep 1" (blocking the
266 process for a second) and then (at time=501) you create a relative
267 timer that fires after three seconds.
268
269 With Event::Lib, "AnyEvent->time" and "AnyEvent->now" will both
270 return 501, because that is the current time, and the timer will be
271 scheduled to fire at time=504 (501 + 3).
272
273 With EV, "AnyEvent->time" returns 501 (as that is the current time),
274 but "AnyEvent->now" returns 500, as that is the time the last event
275 processing phase started. With EV, your timer gets scheduled to run
276 at time=503 (500 + 3).
277
278 In one sense, Event::Lib is more exact, as it uses the current time
279 regardless of any delays introduced by event processing. However,
280 most callbacks do not expect large delays in processing, so this
281 causes a higher drift (and a lot more system calls to get the
282 current time).
283
284 In another sense, EV is more exact, as your timer will be scheduled
285 at the same time, regardless of how long event processing actually
286 took.
287
288 In either case, if you care (and in most cases, you don't), then you
289 can get whatever behaviour you want with any event loop, by taking
290 the difference between "AnyEvent->time" and "AnyEvent->now" into
291 account.
292
225 SIGNAL WATCHERS 293 SIGNAL WATCHERS
226 You can watch for signals using a signal watcher, "signal" is the signal 294 You can watch for signals using a signal watcher, "signal" is the signal
227 *name* without any "SIG" prefix, "cb" is the Perl callback to be invoked 295 *name* without any "SIG" prefix, "cb" is the Perl callback to be invoked
228 whenever a signal occurs. 296 whenever a signal occurs.
229 297
230 Although the callback might get passed parameters, their value and 298 Although the callback might get passed parameters, their value and
231 presence is undefined and you cannot rely on them. Portable AnyEvent 299 presence is undefined and you cannot rely on them. Portable AnyEvent
232 callbacks cannot use arguments passed to signal watcher callbacks. 300 callbacks cannot use arguments passed to signal watcher callbacks.
233 301
234 Multiple signal occurances can be clumped together into one callback 302 Multiple signal occurrences can be clumped together into one callback
235 invocation, and callback invocation will be synchronous. synchronous 303 invocation, and callback invocation will be synchronous. Synchronous
236 means that it might take a while until the signal gets handled by the 304 means that it might take a while until the signal gets handled by the
237 process, but it is guarenteed not to interrupt any other callbacks. 305 process, but it is guaranteed not to interrupt any other callbacks.
238 306
239 The main advantage of using these watchers is that you can share a 307 The main advantage of using these watchers is that you can share a
240 signal between multiple watchers. 308 signal between multiple watchers.
241 309
242 This watcher might use %SIG, so programs overwriting those signals 310 This watcher might use %SIG, so programs overwriting those signals
301 Condition variables can be created by calling the "AnyEvent->condvar" 369 Condition variables can be created by calling the "AnyEvent->condvar"
302 method, usually without arguments. The only argument pair allowed is 370 method, usually without arguments. The only argument pair allowed is
303 "cb", which specifies a callback to be called when the condition 371 "cb", which specifies a callback to be called when the condition
304 variable becomes true. 372 variable becomes true.
305 373
306 After creation, the conditon variable is "false" until it becomes "true" 374 After creation, the condition variable is "false" until it becomes
375 "true" by calling the "send" method (or calling the condition variable
376 as if it were a callback, read about the caveats in the description for
307 by calling the "send" method. 377 the "->send" method).
308 378
309 Condition variables are similar to callbacks, except that you can 379 Condition variables are similar to callbacks, except that you can
310 optionally wait for them. They can also be called merge points - points 380 optionally wait for them. They can also be called merge points - points
311 in time where multiple outstandign events have been processed. And yet 381 in time where multiple outstanding events have been processed. And yet
312 another way to call them is transations - each condition variable can be 382 another way to call them is transactions - each condition variable can
313 used to represent a transaction, which finishes at some point and 383 be used to represent a transaction, which finishes at some point and
314 delivers a result. 384 delivers a result.
315 385
316 Condition variables are very useful to signal that something has 386 Condition variables are very useful to signal that something has
317 finished, for example, if you write a module that does asynchronous http 387 finished, for example, if you write a module that does asynchronous http
318 requests, then a condition variable would be the ideal candidate to 388 requests, then a condition variable would be the ideal candidate to
323 you can block your main program until an event occurs - for example, you 393 you can block your main program until an event occurs - for example, you
324 could "->recv" in your main program until the user clicks the Quit 394 could "->recv" in your main program until the user clicks the Quit
325 button of your app, which would "->send" the "quit" event. 395 button of your app, which would "->send" the "quit" event.
326 396
327 Note that condition variables recurse into the event loop - if you have 397 Note that condition variables recurse into the event loop - if you have
328 two pieces of code that call "->recv" in a round-robbin fashion, you 398 two pieces of code that call "->recv" in a round-robin fashion, you
329 lose. Therefore, condition variables are good to export to your caller, 399 lose. Therefore, condition variables are good to export to your caller,
330 but you should avoid making a blocking wait yourself, at least in 400 but you should avoid making a blocking wait yourself, at least in
331 callbacks, as this asks for trouble. 401 callbacks, as this asks for trouble.
332 402
333 Condition variables are represented by hash refs in perl, and the keys 403 Condition variables are represented by hash refs in perl, and the keys
338 408
339 There are two "sides" to a condition variable - the "producer side" 409 There are two "sides" to a condition variable - the "producer side"
340 which eventually calls "-> send", and the "consumer side", which waits 410 which eventually calls "-> send", and the "consumer side", which waits
341 for the send to occur. 411 for the send to occur.
342 412
343 Example: 413 Example: wait for a timer.
344 414
345 # wait till the result is ready 415 # wait till the result is ready
346 my $result_ready = AnyEvent->condvar; 416 my $result_ready = AnyEvent->condvar;
347 417
348 # do something such as adding a timer 418 # do something such as adding a timer
356 426
357 # this "blocks" (while handling events) till the callback 427 # this "blocks" (while handling events) till the callback
358 # calls send 428 # calls send
359 $result_ready->recv; 429 $result_ready->recv;
360 430
431 Example: wait for a timer, but take advantage of the fact that condition
432 variables are also code references.
433
434 my $done = AnyEvent->condvar;
435 my $delay = AnyEvent->timer (after => 5, cb => $done);
436 $done->recv;
437
361 METHODS FOR PRODUCERS 438 METHODS FOR PRODUCERS
362 These methods should only be used by the producing side, i.e. the 439 These methods should only be used by the producing side, i.e. the
363 code/module that eventually sends the signal. Note that it is also the 440 code/module that eventually sends the signal. Note that it is also the
364 producer side which creates the condvar in most cases, but it isn't 441 producer side which creates the condvar in most cases, but it isn't
365 uncommon for the consumer to create it as well. 442 uncommon for the consumer to create it as well.
372 If a callback has been set on the condition variable, it is called 449 If a callback has been set on the condition variable, it is called
373 immediately from within send. 450 immediately from within send.
374 451
375 Any arguments passed to the "send" call will be returned by all 452 Any arguments passed to the "send" call will be returned by all
376 future "->recv" calls. 453 future "->recv" calls.
454
455 Condition variables are overloaded so one can call them directly (as
456 a code reference). Calling them directly is the same as calling
457 "send". Note, however, that many C-based event loops do not handle
458 overloading, so as tempting as it may be, passing a condition
459 variable instead of a callback does not work. Both the pure perl and
460 EV loops support overloading, however, as well as all functions that
461 use perl to invoke a callback (as in AnyEvent::Socket and
462 AnyEvent::DNS for example).
377 463
378 $cv->croak ($error) 464 $cv->croak ($error)
379 Similar to send, but causes all call's to "->recv" to invoke 465 Similar to send, but causes all call's to "->recv" to invoke
380 "Carp::croak" with the given error message/object/scalar. 466 "Carp::croak" with the given error message/object/scalar.
381 467
427 (the loop doesn't execute once). 513 (the loop doesn't execute once).
428 514
429 This is the general pattern when you "fan out" into multiple 515 This is the general pattern when you "fan out" into multiple
430 subrequests: use an outer "begin"/"end" pair to set the callback and 516 subrequests: use an outer "begin"/"end" pair to set the callback and
431 ensure "end" is called at least once, and then, for each subrequest 517 ensure "end" is called at least once, and then, for each subrequest
432 you start, call "begin" and for eahc subrequest you finish, call 518 you start, call "begin" and for each subrequest you finish, call
433 "end". 519 "end".
434 520
435 METHODS FOR CONSUMERS 521 METHODS FOR CONSUMERS
436 These methods should only be used by the consuming side, i.e. the code 522 These methods should only be used by the consuming side, i.e. the code
437 awaits the condition. 523 awaits the condition.
453 (programs might want to do that to stay interactive), so *if you are 539 (programs might want to do that to stay interactive), so *if you are
454 using this from a module, never require a blocking wait*, but let 540 using this from a module, never require a blocking wait*, but let
455 the caller decide whether the call will block or not (for example, 541 the caller decide whether the call will block or not (for example,
456 by coupling condition variables with some kind of request results 542 by coupling condition variables with some kind of request results
457 and supporting callbacks so the caller knows that getting the result 543 and supporting callbacks so the caller knows that getting the result
458 will not block, while still suppporting blocking waits if the caller 544 will not block, while still supporting blocking waits if the caller
459 so desires). 545 so desires).
460 546
461 Another reason *never* to "->recv" in a module is that you cannot 547 Another reason *never* to "->recv" in a module is that you cannot
462 sensibly have two "->recv"'s in parallel, as that would require 548 sensibly have two "->recv"'s in parallel, as that would require
463 multiple interpreters or coroutines/threads, none of which 549 multiple interpreters or coroutines/threads, none of which
566 If it doesn't care, it can just "use AnyEvent" and use it itself, or not 652 If it doesn't care, it can just "use AnyEvent" and use it itself, or not
567 do anything special (it does not need to be event-based) and let 653 do anything special (it does not need to be event-based) and let
568 AnyEvent decide which implementation to chose if some module relies on 654 AnyEvent decide which implementation to chose if some module relies on
569 it. 655 it.
570 656
571 If the main program relies on a specific event model. For example, in 657 If the main program relies on a specific event model - for example, in
572 Gtk2 programs you have to rely on the Glib module. You should load the 658 Gtk2 programs you have to rely on the Glib module - you should load the
573 event module before loading AnyEvent or any module that uses it: 659 event module before loading AnyEvent or any module that uses it:
574 generally speaking, you should load it as early as possible. The reason 660 generally speaking, you should load it as early as possible. The reason
575 is that modules might create watchers when they are loaded, and AnyEvent 661 is that modules might create watchers when they are loaded, and AnyEvent
576 will decide on the event model to use as soon as it creates watchers, 662 will decide on the event model to use as soon as it creates watchers,
577 and it might chose the wrong one unless you load the correct one 663 and it might chose the wrong one unless you load the correct one
578 yourself. 664 yourself.
579 665
580 You can chose to use a rather inefficient pure-perl implementation by 666 You can chose to use a pure-perl implementation by loading the
581 loading the "AnyEvent::Impl::Perl" module, which gives you similar 667 "AnyEvent::Impl::Perl" module, which gives you similar behaviour
582 behaviour everywhere, but letting AnyEvent chose is generally better. 668 everywhere, but letting AnyEvent chose the model is generally better.
669
670 MAINLOOP EMULATION
671 Sometimes (often for short test scripts, or even standalone programs who
672 only want to use AnyEvent), you do not want to run a specific event
673 loop.
674
675 In that case, you can use a condition variable like this:
676
677 AnyEvent->condvar->recv;
678
679 This has the effect of entering the event loop and looping forever.
680
681 Note that usually your program has some exit condition, in which case it
682 is better to use the "traditional" approach of storing a condition
683 variable somewhere, waiting for it, and sending it when the program
684 should exit cleanly.
583 685
584OTHER MODULES 686OTHER MODULES
585 The following is a non-exhaustive list of additional modules that use 687 The following is a non-exhaustive list of additional modules that use
586 AnyEvent and can therefore be mixed easily with other AnyEvent modules 688 AnyEvent and can therefore be mixed easily with other AnyEvent modules
587 in the same program. Some of the modules come with AnyEvent, some are 689 in the same program. Some of the modules come with AnyEvent, some are
594 696
595 AnyEvent::Handle 697 AnyEvent::Handle
596 Provide read and write buffers and manages watchers for reads and 698 Provide read and write buffers and manages watchers for reads and
597 writes. 699 writes.
598 700
701 AnyEvent::Socket
702 Provides various utility functions for (internet protocol) sockets,
703 addresses and name resolution. Also functions to create non-blocking
704 tcp connections or tcp servers, with IPv6 and SRV record support and
705 more.
706
707 AnyEvent::DNS
708 Provides rich asynchronous DNS resolver capabilities.
709
599 AnyEvent::HTTPD 710 AnyEvent::HTTPD
600 Provides a simple web application server framework. 711 Provides a simple web application server framework.
601
602 AnyEvent::DNS
603 Provides asynchronous DNS resolver capabilities, beyond what
604 AnyEvent::Util offers.
605 712
606 AnyEvent::FastPing 713 AnyEvent::FastPing
607 The fastest ping in the west. 714 The fastest ping in the west.
608 715
609 Net::IRC3 716 Net::IRC3
693 When set to 2 or higher, cause AnyEvent to report to STDERR which 800 When set to 2 or higher, cause AnyEvent to report to STDERR which
694 event model it chooses. 801 event model it chooses.
695 802
696 "PERL_ANYEVENT_MODEL" 803 "PERL_ANYEVENT_MODEL"
697 This can be used to specify the event model to be used by AnyEvent, 804 This can be used to specify the event model to be used by AnyEvent,
698 before autodetection and -probing kicks in. It must be a string 805 before auto detection and -probing kicks in. It must be a string
699 consisting entirely of ASCII letters. The string "AnyEvent::Impl::" 806 consisting entirely of ASCII letters. The string "AnyEvent::Impl::"
700 gets prepended and the resulting module name is loaded and if the 807 gets prepended and the resulting module name is loaded and if the
701 load was successful, used as event model. If it fails to load 808 load was successful, used as event model. If it fails to load
702 AnyEvent will proceed with autodetection and -probing. 809 AnyEvent will proceed with auto detection and -probing.
703 810
704 This functionality might change in future versions. 811 This functionality might change in future versions.
705 812
706 For example, to force the pure perl model (AnyEvent::Impl::Perl) you 813 For example, to force the pure perl model (AnyEvent::Impl::Perl) you
707 could start your program like this: 814 could start your program like this:
708 815
709 PERL_ANYEVENT_MODEL=Perl perl ... 816 PERL_ANYEVENT_MODEL=Perl perl ...
817
818 "PERL_ANYEVENT_PROTOCOLS"
819 Used by both AnyEvent::DNS and AnyEvent::Socket to determine
820 preferences for IPv4 or IPv6. The default is unspecified (and might
821 change, or be the result of auto probing).
822
823 Must be set to a comma-separated list of protocols or address
824 families, current supported: "ipv4" and "ipv6". Only protocols
825 mentioned will be used, and preference will be given to protocols
826 mentioned earlier in the list.
827
828 This variable can effectively be used for denial-of-service attacks
829 against local programs (e.g. when setuid), although the impact is
830 likely small, as the program has to handle connection errors
831 already-
832
833 Examples: "PERL_ANYEVENT_PROTOCOLS=ipv4,ipv6" - prefer IPv4 over
834 IPv6, but support both and try to use both.
835 "PERL_ANYEVENT_PROTOCOLS=ipv4" - only support IPv4, never try to
836 resolve or contact IPv6 addresses.
837 "PERL_ANYEVENT_PROTOCOLS=ipv6,ipv4" support either IPv4 or IPv6, but
838 prefer IPv6 over IPv4.
839
840 "PERL_ANYEVENT_EDNS0"
841 Used by AnyEvent::DNS to decide whether to use the EDNS0 extension
842 for DNS. This extension is generally useful to reduce DNS traffic,
843 but some (broken) firewalls drop such DNS packets, which is why it
844 is off by default.
845
846 Setting this variable to 1 will cause AnyEvent::DNS to announce
847 EDNS0 in its DNS requests.
848
849 "PERL_ANYEVENT_MAX_FORKS"
850 The maximum number of child processes that
851 "AnyEvent::Util::fork_call" will create in parallel.
710 852
711EXAMPLE PROGRAM 853EXAMPLE PROGRAM
712 The following program uses an I/O watcher to read data from STDIN, a 854 The following program uses an I/O watcher to read data from STDIN, a
713 timer to display a message once per second, and a condition variable to 855 timer to display a message once per second, and a condition variable to
714 quit the program when the user enters quit: 856 quit the program when the user enters quit:
796 syswrite $txn->{fh}, $txn->{request} 938 syswrite $txn->{fh}, $txn->{request}
797 or die "connection or write error"; 939 or die "connection or write error";
798 $txn->{w} = AnyEvent->io (fh => $txn->{fh}, poll => 'r', cb => sub { $txn->fh_ready_r }); 940 $txn->{w} = AnyEvent->io (fh => $txn->{fh}, poll => 'r', cb => sub { $txn->fh_ready_r });
799 941
800 Again, "fh_ready_r" waits till all data has arrived, and then stores the 942 Again, "fh_ready_r" waits till all data has arrived, and then stores the
801 result and signals any possible waiters that the request ahs finished: 943 result and signals any possible waiters that the request has finished:
802 944
803 sysread $txn->{fh}, $txn->{buf}, length $txn->{$buf}; 945 sysread $txn->{fh}, $txn->{buf}, length $txn->{$buf};
804 946
805 if (end-of-file or data complete) { 947 if (end-of-file or data complete) {
806 $txn->{result} = $txn->{buf}; 948 $txn->{result} = $txn->{buf};
814 956
815 $txn->{finished}->recv; 957 $txn->{finished}->recv;
816 return $txn->{result}; 958 return $txn->{result};
817 959
818 The actual code goes further and collects all errors ("die"s, 960 The actual code goes further and collects all errors ("die"s,
819 exceptions) that occured during request processing. The "result" method 961 exceptions) that occurred during request processing. The "result" method
820 detects whether an exception as thrown (it is stored inside the $txn 962 detects whether an exception as thrown (it is stored inside the $txn
821 object) and just throws the exception, which means connection errors and 963 object) and just throws the exception, which means connection errors and
822 other problems get reported tot he code that tries to use the result, 964 other problems get reported tot he code that tries to use the result,
823 not in a random callback. 965 not in a random callback.
824 966
867 over the event loops themselves and to give you an impression of the 1009 over the event loops themselves and to give you an impression of the
868 speed of various event loops I prepared some benchmarks. 1010 speed of various event loops I prepared some benchmarks.
869 1011
870 BENCHMARKING ANYEVENT OVERHEAD 1012 BENCHMARKING ANYEVENT OVERHEAD
871 Here is a benchmark of various supported event models used natively and 1013 Here is a benchmark of various supported event models used natively and
872 through anyevent. The benchmark creates a lot of timers (with a zero 1014 through AnyEvent. The benchmark creates a lot of timers (with a zero
873 timeout) and I/O watchers (watching STDOUT, a pty, to become writable, 1015 timeout) and I/O watchers (watching STDOUT, a pty, to become writable,
874 which it is), lets them fire exactly once and destroys them again. 1016 which it is), lets them fire exactly once and destroys them again.
875 1017
876 Source code for this benchmark is found as eg/bench in the AnyEvent 1018 Source code for this benchmark is found as eg/bench in the AnyEvent
877 distribution. 1019 distribution.
992 1134
993 * You should avoid POE like the plague if you want performance or 1135 * You should avoid POE like the plague if you want performance or
994 reasonable memory usage. 1136 reasonable memory usage.
995 1137
996 BENCHMARKING THE LARGE SERVER CASE 1138 BENCHMARKING THE LARGE SERVER CASE
997 This benchmark atcually benchmarks the event loop itself. It works by 1139 This benchmark actually benchmarks the event loop itself. It works by
998 creating a number of "servers": each server consists of a socketpair, a 1140 creating a number of "servers": each server consists of a socket pair, a
999 timeout watcher that gets reset on activity (but never fires), and an 1141 timeout watcher that gets reset on activity (but never fires), and an
1000 I/O watcher waiting for input on one side of the socket. Each time the 1142 I/O watcher waiting for input on one side of the socket. Each time the
1001 socket watcher reads a byte it will write that byte to a random other 1143 socket watcher reads a byte it will write that byte to a random other
1002 "server". 1144 "server".
1003 1145
1004 The effect is that there will be a lot of I/O watchers, only part of 1146 The effect is that there will be a lot of I/O watchers, only part of
1005 which are active at any one point (so there is a constant number of 1147 which are active at any one point (so there is a constant number of
1006 active fds for each loop iterstaion, but which fds these are is random). 1148 active fds for each loop iteration, but which fds these are is random).
1007 The timeout is reset each time something is read because that reflects 1149 The timeout is reset each time something is read because that reflects
1008 how most timeouts work (and puts extra pressure on the event loops). 1150 how most timeouts work (and puts extra pressure on the event loops).
1009 1151
1010 In this benchmark, we use 10000 socketpairs (20000 sockets), of which 1152 In this benchmark, we use 10000 socket pairs (20000 sockets), of which
1011 100 (1%) are active. This mirrors the activity of large servers with 1153 100 (1%) are active. This mirrors the activity of large servers with
1012 many connections, most of which are idle at any one point in time. 1154 many connections, most of which are idle at any one point in time.
1013 1155
1014 Source code for this benchmark is found as eg/bench2 in the AnyEvent 1156 Source code for this benchmark is found as eg/bench2 in the AnyEvent
1015 distribution. 1157 distribution.
1016 1158
1017 Explanation of the columns 1159 Explanation of the columns
1018 *sockets* is the number of sockets, and twice the number of "servers" 1160 *sockets* is the number of sockets, and twice the number of "servers"
1019 (as each server has a read and write socket end). 1161 (as each server has a read and write socket end).
1020 1162
1021 *create* is the time it takes to create a socketpair (which is 1163 *create* is the time it takes to create a socket pair (which is
1022 nontrivial) and two watchers: an I/O watcher and a timeout watcher. 1164 nontrivial) and two watchers: an I/O watcher and a timeout watcher.
1023 1165
1024 *request*, the most important value, is the time it takes to handle a 1166 *request*, the most important value, is the time it takes to handle a
1025 single "request", that is, reading the token from the pipe and 1167 single "request", that is, reading the token from the pipe and
1026 forwarding it to another server. This includes deleting the old timeout 1168 forwarding it to another server. This includes deleting the old timeout
1090 and speed most when you have lots of watchers, not when you only have a 1232 and speed most when you have lots of watchers, not when you only have a
1091 few of them). 1233 few of them).
1092 1234
1093 EV is again fastest. 1235 EV is again fastest.
1094 1236
1095 Perl again comes second. It is noticably faster than the C-based event 1237 Perl again comes second. It is noticeably faster than the C-based event
1096 loops Event and Glib, although the difference is too small to really 1238 loops Event and Glib, although the difference is too small to really
1097 matter. 1239 matter.
1098 1240
1099 POE also performs much better in this case, but is is still far behind 1241 POE also performs much better in this case, but is is still far behind
1100 the others. 1242 the others.
1129 Similar considerations apply to $ENV{PERL_ANYEVENT_VERBOSE}, as that can 1271 Similar considerations apply to $ENV{PERL_ANYEVENT_VERBOSE}, as that can
1130 be used to probe what backend is used and gain other information (which 1272 be used to probe what backend is used and gain other information (which
1131 is probably even less useful to an attacker than PERL_ANYEVENT_MODEL). 1273 is probably even less useful to an attacker than PERL_ANYEVENT_MODEL).
1132 1274
1133SEE ALSO 1275SEE ALSO
1276 Utility functions: AnyEvent::Util.
1277
1134 Event modules: EV, EV::Glib, Glib::EV, Event, Glib::Event, Glib, Tk, 1278 Event modules: EV, EV::Glib, Glib::EV, Event, Glib::Event, Glib, Tk,
1135 Event::Lib, Qt, POE. 1279 Event::Lib, Qt, POE.
1136 1280
1137 Implementations: AnyEvent::Impl::EV, AnyEvent::Impl::Event, 1281 Implementations: AnyEvent::Impl::EV, AnyEvent::Impl::Event,
1138 AnyEvent::Impl::Glib, AnyEvent::Impl::Tk, AnyEvent::Impl::Perl, 1282 AnyEvent::Impl::Glib, AnyEvent::Impl::Tk, AnyEvent::Impl::Perl,
1139 AnyEvent::Impl::EventLib, AnyEvent::Impl::Qt, AnyEvent::Impl::POE. 1283 AnyEvent::Impl::EventLib, AnyEvent::Impl::Qt, AnyEvent::Impl::POE.
1140 1284
1285 Non-blocking file handles, sockets, TCP clients and servers:
1286 AnyEvent::Handle, AnyEvent::Socket.
1287
1288 Asynchronous DNS: AnyEvent::DNS.
1289
1141 Coroutine support: Coro, Coro::AnyEvent, Coro::EV, Coro::Event, 1290 Coroutine support: Coro, Coro::AnyEvent, Coro::EV, Coro::Event,
1142 1291
1143 Nontrivial usage examples: Net::FCP, Net::XMPP2. 1292 Nontrivial usage examples: Net::FCP, Net::XMPP2, AnyEvent::DNS.
1144 1293
1145AUTHOR 1294AUTHOR
1146 Marc Lehmann <schmorp@schmorp.de> 1295 Marc Lehmann <schmorp@schmorp.de>
1147 http://home.schmorp.de/ 1296 http://home.schmorp.de/
1148 1297

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines