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

Comparing AnyEvent/README (file contents):
Revision 1.28 by root, Sat Jul 12 20:45:27 2008 UTC vs.
Revision 1.29 by root, Tue Jul 29 10:20:33 2008 UTC

5 loops 5 loops
6 6
7SYNOPSIS 7SYNOPSIS
8 use AnyEvent; 8 use AnyEvent;
9 9
10 my $w = AnyEvent->io (fh => $fh, poll => "r|w", cb => sub { 10 my $w = AnyEvent->io (fh => $fh, poll => "r|w", cb => sub { ... });
11 ...
12 });
13 11
14 my $w = AnyEvent->timer (after => $seconds, cb => sub { 12 my $w = AnyEvent->timer (after => $seconds, cb => sub { ... });
13 my $w = AnyEvent->timer (after => $seconds, interval => $seconds, cb => ...
14
15 print AnyEvent->now; # prints current event loop time
16 print AnyEvent->time; # think Time::HiRes::time or simply CORE::time.
17
18 my $w = AnyEvent->signal (signal => "TERM", cb => sub { ... });
19
20 my $w = AnyEvent->child (pid => $pid, cb => sub {
21 my ($pid, $status) = @_;
15 ... 22 ...
16 }); 23 });
17 24
18 my $w = AnyEvent->condvar; # stores whether a condition was flagged 25 my $w = AnyEvent->condvar; # stores whether a condition was flagged
19 $w->send; # wake up current and all future recv's 26 $w->send; # wake up current and all future recv's
20 $w->recv; # enters "main loop" till $condvar gets ->send 27 $w->recv; # enters "main loop" till $condvar gets ->send
28 # use a condvar in callback mode:
29 $w->cb (sub { $_[0]->recv });
21 30
22INTRODUCTION/TUTORIAL 31INTRODUCTION/TUTORIAL
23 This manpage is mainly a reference manual. If you are interested in a 32 This manpage is mainly a reference manual. If you are interested in a
24 tutorial or some gentle introduction, have a look at the AnyEvent::Intro 33 tutorial or some gentle introduction, have a look at the AnyEvent::Intro
25 manpage. 34 manpage.
371 The instrument to do that is called a "condition variable", so called 380 The instrument to do that is called a "condition variable", so called
372 because they represent a condition that must become true. 381 because they represent a condition that must become true.
373 382
374 Condition variables can be created by calling the "AnyEvent->condvar" 383 Condition variables can be created by calling the "AnyEvent->condvar"
375 method, usually without arguments. The only argument pair allowed is 384 method, usually without arguments. The only argument pair allowed is
385
376 "cb", which specifies a callback to be called when the condition 386 "cb", which specifies a callback to be called when the condition
377 variable becomes true. 387 variable becomes true, with the condition variable as the first argument
388 (but not the results).
378 389
379 After creation, the condition variable is "false" until it becomes 390 After creation, the condition variable is "false" until it becomes
380 "true" by calling the "send" method (or calling the condition variable 391 "true" by calling the "send" method (or calling the condition variable
381 as if it were a callback, read about the caveats in the description for 392 as if it were a callback, read about the caveats in the description for
382 the "->send" method). 393 the "->send" method).
438 449
439 my $done = AnyEvent->condvar; 450 my $done = AnyEvent->condvar;
440 my $delay = AnyEvent->timer (after => 5, cb => $done); 451 my $delay = AnyEvent->timer (after => 5, cb => $done);
441 $done->recv; 452 $done->recv;
442 453
454 Example: Imagine an API that returns a condvar and doesn't support
455 callbacks. This is how you make a synchronous call, for example from the
456 main program:
457
458 use AnyEvent::CouchDB;
459
460 ...
461
462 my @info = $couchdb->info->recv;
463
464 And this is how you would just ste a callback to be called whenever the
465 results are available:
466
467 $couchdb->info->cb (sub {
468 my @info = $_[0]->recv;
469 });
470
443 METHODS FOR PRODUCERS 471 METHODS FOR PRODUCERS
444 These methods should only be used by the producing side, i.e. the 472 These methods should only be used by the producing side, i.e. the
445 code/module that eventually sends the signal. Note that it is also the 473 code/module that eventually sends the signal. Note that it is also the
446 producer side which creates the condvar in most cases, but it isn't 474 producer side which creates the condvar in most cases, but it isn't
447 uncommon for the consumer to create it as well. 475 uncommon for the consumer to create it as well.
567 595
568 $bool = $cv->ready 596 $bool = $cv->ready
569 Returns true when the condition is "true", i.e. whether "send" or 597 Returns true when the condition is "true", i.e. whether "send" or
570 "croak" have been called. 598 "croak" have been called.
571 599
572 $cb = $cv->cb ([new callback]) 600 $cb = $cv->cb ($cb->($cv))
573 This is a mutator function that returns the callback set and 601 This is a mutator function that returns the callback set and
574 optionally replaces it before doing so. 602 optionally replaces it before doing so.
575 603
576 The callback will be called when the condition becomes "true", i.e. 604 The callback will be called when the condition becomes "true", i.e.
577 when "send" or "croak" are called, with the only argument being the 605 when "send" or "croak" are called, with the only argument being the

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines