… | |
… | |
609 | eventually calls C<< -> send >>, and the "consumer side", which waits |
609 | eventually calls C<< -> send >>, and the "consumer side", which waits |
610 | for the send to occur. |
610 | for the send to occur. |
611 | |
611 | |
612 | Example: wait for a timer. |
612 | Example: wait for a timer. |
613 | |
613 | |
614 | # wait till the result is ready |
614 | # condition: "wait till the timer is fired" |
615 | my $result_ready = AnyEvent->condvar; |
615 | my $timer_fired = AnyEvent->condvar; |
616 | |
616 | |
617 | # do something such as adding a timer |
617 | # create the timer - we could wait for, say |
618 | # or socket watcher the calls $result_ready->send |
618 | # a handle becomign ready, or even an |
619 | # when the "result" is ready. |
619 | # AnyEvent::HTTP request to finish, but |
620 | # in this case, we simply use a timer: |
620 | # in this case, we simply use a timer: |
621 | my $w = AnyEvent->timer ( |
621 | my $w = AnyEvent->timer ( |
622 | after => 1, |
622 | after => 1, |
623 | cb => sub { $result_ready->send }, |
623 | cb => sub { $timer_fired->send }, |
624 | ); |
624 | ); |
625 | |
625 | |
626 | # this "blocks" (while handling events) till the callback |
626 | # this "blocks" (while handling events) till the callback |
627 | # calls ->send |
627 | # calls ->send |
628 | $result_ready->recv; |
628 | $timer_fired->recv; |
629 | |
629 | |
630 | Example: wait for a timer, but take advantage of the fact that condition |
630 | Example: wait for a timer, but take advantage of the fact that condition |
631 | variables are also callable directly. |
631 | variables are also callable directly. |
632 | |
632 | |
633 | my $done = AnyEvent->condvar; |
633 | my $done = AnyEvent->condvar; |