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.128 by root, Sat May 24 02:50:45 2008 UTC vs.
Revision 1.134 by root, Sun May 25 04:44:04 2008 UTC

237 237
238Although the callback might get passed parameters, their value and 238Although the callback might get passed parameters, their value and
239presence is undefined and you cannot rely on them. Portable AnyEvent 239presence is undefined and you cannot rely on them. Portable AnyEvent
240callbacks cannot use arguments passed to signal watcher callbacks. 240callbacks cannot use arguments passed to signal watcher callbacks.
241 241
242Multiple signal occurances can be clumped together into one callback 242Multiple signal occurrences can be clumped together into one callback
243invocation, and callback invocation will be synchronous. synchronous means 243invocation, and callback invocation will be synchronous. Synchronous means
244that it might take a while until the signal gets handled by the process, 244that it might take a while until the signal gets handled by the process,
245but it is guarenteed not to interrupt any other callbacks. 245but it is guaranteed not to interrupt any other callbacks.
246 246
247The main advantage of using these watchers is that you can share a signal 247The main advantage of using these watchers is that you can share a signal
248between multiple watchers. 248between multiple watchers.
249 249
250This watcher might use C<%SIG>, so programs overwriting those signals 250This watcher might use C<%SIG>, so programs overwriting those signals
310Condition variables can be created by calling the C<< AnyEvent->condvar 310Condition variables can be created by calling the C<< AnyEvent->condvar
311>> method, usually without arguments. The only argument pair allowed is 311>> method, usually without arguments. The only argument pair allowed is
312C<cb>, which specifies a callback to be called when the condition variable 312C<cb>, which specifies a callback to be called when the condition variable
313becomes true. 313becomes true.
314 314
315After creation, the conditon variable is "false" until it becomes "true" 315After creation, the condition variable is "false" until it becomes "true"
316by calling the C<send> method. 316by calling the C<send> method (or calling the condition variable as if it
317were a callback).
317 318
318Condition variables are similar to callbacks, except that you can 319Condition variables are similar to callbacks, except that you can
319optionally wait for them. They can also be called merge points - points 320optionally wait for them. They can also be called merge points - points
320in time where multiple outstandign events have been processed. And yet 321in time where multiple outstanding events have been processed. And yet
321another way to call them is transations - each condition variable can be 322another way to call them is transactions - each condition variable can be
322used to represent a transaction, which finishes at some point and delivers 323used to represent a transaction, which finishes at some point and delivers
323a result. 324a result.
324 325
325Condition variables are very useful to signal that something has finished, 326Condition variables are very useful to signal that something has finished,
326for example, if you write a module that does asynchronous http requests, 327for example, if you write a module that does asynchronous http requests,
332you can block your main program until an event occurs - for example, you 333you can block your main program until an event occurs - for example, you
333could C<< ->recv >> in your main program until the user clicks the Quit 334could C<< ->recv >> in your main program until the user clicks the Quit
334button of your app, which would C<< ->send >> the "quit" event. 335button of your app, which would C<< ->send >> the "quit" event.
335 336
336Note that condition variables recurse into the event loop - if you have 337Note that condition variables recurse into the event loop - if you have
337two pieces of code that call C<< ->recv >> in a round-robbin fashion, you 338two pieces of code that call C<< ->recv >> in a round-robin fashion, you
338lose. Therefore, condition variables are good to export to your caller, but 339lose. Therefore, condition variables are good to export to your caller, but
339you should avoid making a blocking wait yourself, at least in callbacks, 340you should avoid making a blocking wait yourself, at least in callbacks,
340as this asks for trouble. 341as this asks for trouble.
341 342
342Condition variables are represented by hash refs in perl, and the keys 343Condition variables are represented by hash refs in perl, and the keys
347 348
348There are two "sides" to a condition variable - the "producer side" which 349There are two "sides" to a condition variable - the "producer side" which
349eventually calls C<< -> send >>, and the "consumer side", which waits 350eventually calls C<< -> send >>, and the "consumer side", which waits
350for the send to occur. 351for the send to occur.
351 352
352Example: 353Example: wait for a timer.
353 354
354 # wait till the result is ready 355 # wait till the result is ready
355 my $result_ready = AnyEvent->condvar; 356 my $result_ready = AnyEvent->condvar;
356 357
357 # do something such as adding a timer 358 # do something such as adding a timer
365 366
366 # this "blocks" (while handling events) till the callback 367 # this "blocks" (while handling events) till the callback
367 # calls send 368 # calls send
368 $result_ready->recv; 369 $result_ready->recv;
369 370
371Example: wait for a timer, but take advantage of the fact that
372condition variables are also code references.
373
374 my $done = AnyEvent->condvar;
375 my $delay = AnyEvent->timer (after => 5, cb => $done);
376 $done->recv;
377
370=head3 METHODS FOR PRODUCERS 378=head3 METHODS FOR PRODUCERS
371 379
372These methods should only be used by the producing side, i.e. the 380These methods should only be used by the producing side, i.e. the
373code/module that eventually sends the signal. Note that it is also 381code/module that eventually sends the signal. Note that it is also
374the producer side which creates the condvar in most cases, but it isn't 382the producer side which creates the condvar in most cases, but it isn't
385If a callback has been set on the condition variable, it is called 393If a callback has been set on the condition variable, it is called
386immediately from within send. 394immediately from within send.
387 395
388Any arguments passed to the C<send> call will be returned by all 396Any arguments passed to the C<send> call will be returned by all
389future C<< ->recv >> calls. 397future C<< ->recv >> calls.
398
399Condition variables are overloaded so one can call them directly (as a
400code reference). Calling them directly is the same as calling C<send>.
390 401
391=item $cv->croak ($error) 402=item $cv->croak ($error)
392 403
393Similar to send, but causes all call's to C<< ->recv >> to invoke 404Similar to send, but causes all call's to C<< ->recv >> to invoke
394C<Carp::croak> with the given error message/object/scalar. 405C<Carp::croak> with the given error message/object/scalar.
443doesn't execute once). 454doesn't execute once).
444 455
445This is the general pattern when you "fan out" into multiple subrequests: 456This is the general pattern when you "fan out" into multiple subrequests:
446use an outer C<begin>/C<end> pair to set the callback and ensure C<end> 457use an outer C<begin>/C<end> pair to set the callback and ensure C<end>
447is called at least once, and then, for each subrequest you start, call 458is called at least once, and then, for each subrequest you start, call
448C<begin> and for eahc subrequest you finish, call C<end>. 459C<begin> and for each subrequest you finish, call C<end>.
449 460
450=back 461=back
451 462
452=head3 METHODS FOR CONSUMERS 463=head3 METHODS FOR CONSUMERS
453 464
475(programs might want to do that to stay interactive), so I<if you are 486(programs might want to do that to stay interactive), so I<if you are
476using this from a module, never require a blocking wait>, but let the 487using this from a module, never require a blocking wait>, but let the
477caller decide whether the call will block or not (for example, by coupling 488caller decide whether the call will block or not (for example, by coupling
478condition variables with some kind of request results and supporting 489condition variables with some kind of request results and supporting
479callbacks so the caller knows that getting the result will not block, 490callbacks so the caller knows that getting the result will not block,
480while still suppporting blocking waits if the caller so desires). 491while still supporting blocking waits if the caller so desires).
481 492
482Another reason I<never> to C<< ->recv >> in a module is that you cannot 493Another reason I<never> to C<< ->recv >> in a module is that you cannot
483sensibly have two C<< ->recv >>'s in parallel, as that would require 494sensibly have two C<< ->recv >>'s in parallel, as that would require
484multiple interpreters or coroutines/threads, none of which C<AnyEvent> 495multiple interpreters or coroutines/threads, none of which C<AnyEvent>
485can supply. 496can supply.
601 612
602If it doesn't care, it can just "use AnyEvent" and use it itself, or not 613If it doesn't care, it can just "use AnyEvent" and use it itself, or not
603do anything special (it does not need to be event-based) and let AnyEvent 614do anything special (it does not need to be event-based) and let AnyEvent
604decide which implementation to chose if some module relies on it. 615decide which implementation to chose if some module relies on it.
605 616
606If the main program relies on a specific event model. For example, in 617If the main program relies on a specific event model - for example, in
607Gtk2 programs you have to rely on the Glib module. You should load the 618Gtk2 programs you have to rely on the Glib module - you should load the
608event module before loading AnyEvent or any module that uses it: generally 619event module before loading AnyEvent or any module that uses it: generally
609speaking, you should load it as early as possible. The reason is that 620speaking, you should load it as early as possible. The reason is that
610modules might create watchers when they are loaded, and AnyEvent will 621modules might create watchers when they are loaded, and AnyEvent will
611decide on the event model to use as soon as it creates watchers, and it 622decide on the event model to use as soon as it creates watchers, and it
612might chose the wrong one unless you load the correct one yourself. 623might chose the wrong one unless you load the correct one yourself.
613 624
614You can chose to use a rather inefficient pure-perl implementation by 625You can chose to use a pure-perl implementation by loading the
615loading the C<AnyEvent::Impl::Perl> module, which gives you similar 626C<AnyEvent::Impl::Perl> module, which gives you similar behaviour
616behaviour everywhere, but letting AnyEvent chose is generally better. 627everywhere, but letting AnyEvent chose the model is generally better.
628
629=head2 MAINLOOP EMULATION
630
631Sometimes (often for short test scripts, or even standalone programs who
632only want to use AnyEvent), you do not want to run a specific event loop.
633
634In that case, you can use a condition variable like this:
635
636 AnyEvent->condvar->recv;
637
638This has the effect of entering the event loop and looping forever.
639
640Note that usually your program has some exit condition, in which case
641it is better to use the "traditional" approach of storing a condition
642variable somewhere, waiting for it, and sending it when the program should
643exit cleanly.
644
617 645
618=head1 OTHER MODULES 646=head1 OTHER MODULES
619 647
620The following is a non-exhaustive list of additional modules that use 648The following is a non-exhaustive list of additional modules that use
621AnyEvent and can therefore be mixed easily with other AnyEvent modules 649AnyEvent and can therefore be mixed easily with other AnyEvent modules
637 665
638Provides various utility functions for (internet protocol) sockets, 666Provides various utility functions for (internet protocol) sockets,
639addresses and name resolution. Also functions to create non-blocking tcp 667addresses and name resolution. Also functions to create non-blocking tcp
640connections or tcp servers, with IPv6 and SRV record support and more. 668connections or tcp servers, with IPv6 and SRV record support and more.
641 669
670=item L<AnyEvent::DNS>
671
672Provides rich asynchronous DNS resolver capabilities.
673
642=item L<AnyEvent::HTTPD> 674=item L<AnyEvent::HTTPD>
643 675
644Provides a simple web application server framework. 676Provides a simple web application server framework.
645
646=item L<AnyEvent::DNS>
647
648Provides rich asynchronous DNS resolver capabilities.
649 677
650=item L<AnyEvent::FastPing> 678=item L<AnyEvent::FastPing>
651 679
652The fastest ping in the west. 680The fastest ping in the west.
653 681
696no warnings; 724no warnings;
697use strict; 725use strict;
698 726
699use Carp; 727use Carp;
700 728
701our $VERSION = '3.6'; 729our $VERSION = '4.03';
702our $MODEL; 730our $MODEL;
703 731
704our $AUTOLOAD; 732our $AUTOLOAD;
705our @ISA; 733our @ISA;
706 734
914 942
915our @ISA = AnyEvent::CondVar::Base::; 943our @ISA = AnyEvent::CondVar::Base::;
916 944
917package AnyEvent::CondVar::Base; 945package AnyEvent::CondVar::Base;
918 946
947use overload
948 '&{}' => sub { my $self = shift; sub { $self->send (@_) } },
949 fallback => 1;
950
919sub _send { 951sub _send {
920 # nop 952 # nop
921} 953}
922 954
923sub send { 955sub send {
1481speed most when you have lots of watchers, not when you only have a few of 1513speed most when you have lots of watchers, not when you only have a few of
1482them). 1514them).
1483 1515
1484EV is again fastest. 1516EV is again fastest.
1485 1517
1486Perl again comes second. It is noticably faster than the C-based event 1518Perl again comes second. It is noticeably faster than the C-based event
1487loops Event and Glib, although the difference is too small to really 1519loops Event and Glib, although the difference is too small to really
1488matter. 1520matter.
1489 1521
1490POE also performs much better in this case, but is is still far behind the 1522POE also performs much better in this case, but is is still far behind the
1491others. 1523others.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines