~ubuntu-branches/ubuntu/trusty/libanyevent-perl/trusty

« back to all changes in this revision

Viewing changes to lib/AnyEvent/FAQ.pod

  • Committer: Bazaar Package Importer
  • Author(s): Nicholas Bamber, Nicholas Bamber, Ansgar Burchardt
  • Date: 2010-10-20 21:57:18 UTC
  • mfrom: (1.4.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20101020215718-xvywm1g8a80sf438
Tags: 5.280-1
[ Nicholas Bamber ]
* Added myself to Uploaders 
* New upstream release
* Rewrote short description
* Refreshed copyright: spec line and remove 'GNU/Linux'

[ Ansgar Burchardt ]
* Update my email address.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
=head1 NAME
 
2
 
 
3
AnyEvent::FAQ - frequently asked questions
 
4
 
 
5
The newest version of this document can be found at
 
6
L<http://pod.tst.eu/http://cvs.schmorp.de/AnyEvent/lib/AnyEvent/FAQ.pod>.
 
7
 
 
8
=head2 My program exits before doing anything, what's going on?
 
9
 
 
10
Programmers new to event-based programming often forget that you can
 
11
actually do other stuff while "waiting" for an event to occur and
 
12
therefore forget to actually wait when they do not, in fact, have anything
 
13
else to do.
 
14
 
 
15
Here is an example:
 
16
 
 
17
   use AnyEvent;
 
18
 
 
19
   my $timer = AnyEvent->timer (after => 5, cb => sub { say "hi" });
 
20
 
 
21
The expectation might be for the program to print "hi" after 5 seconds
 
22
and then probably to exit. However, if you run this, your program will
 
23
exit almost instantly: Creating the timer does not wait for it, instead
 
24
the C<timer> method returns immediately and perl executes the rest of the
 
25
program. But there is nothing left to execute, so perl exits.
 
26
 
 
27
To force AnyEvent to wait for something, use a condvar:
 
28
 
 
29
   use AnyEvent;
 
30
 
 
31
   my $quit_program = AnyEvent->condvar;
 
32
   my $timer = AnyEvent->timer (after => 5, cb => sub { $quit_program->send });
 
33
 
 
34
   $quit_program->recv;
 
35
 
 
36
Here the program doesn't immediately exit, because it first waits for
 
37
the "quit_program" condition.
 
38
 
 
39
In most cases, your main program should call the event library "loop"
 
40
function directly:
 
41
 
 
42
   use EV;
 
43
   use AnyEvent;
 
44
 
 
45
   ...
 
46
 
 
47
   EV::loop;
 
48
 
 
49
=head2 Why is my C<tcp_connect> callback never called?
 
50
 
 
51
Tricky: C<tcp_connect> (and a few other functions in L<AnyEvent::Socket>)
 
52
is critically sensitive to the caller context.
 
53
 
 
54
In void context, it will just do it's thing and eventually call the
 
55
callback. In any other context, however, it will return a special "guard"
 
56
object - when it is destroyed (e.g. when you don't store it but throw it
 
57
away), tcp_connect will no longer try to connect or call any callbacks.
 
58
 
 
59
Often this happens when the C<tcp_connect> call is at the end of a function:
 
60
 
 
61
   sub do_connect {
 
62
      tcp_connect "www.example.com", 80, sub {
 
63
         ... lengthy code
 
64
      };
 
65
   }
 
66
 
 
67
Then the caller decides whether there is a void context or not. One can
 
68
avoid these cases by explicitly returning nothing:
 
69
 
 
70
   sub do_connect {
 
71
      tcp_connect "www.example.com", 80, sub {
 
72
         ... lengthy code
 
73
      };
 
74
 
 
75
      () # return nothing
 
76
   }
 
77
 
 
78
=head2 Why do some backends use a lot of CPU in C<< AE::cv->recv >>?
 
79
 
 
80
Many people try out this simple program, or it's equivalent:
 
81
 
 
82
   AnyEvent->condvar->recv;
 
83
 
 
84
They are then shocked to see that this basically idles with the Perl
 
85
backend, but uses 100% CPU with the EV backend, which is supposed to be
 
86
sooo efficient.
 
87
 
 
88
The key to understand this is to understand that the above program is
 
89
actually buggy: Nothing calls C<< ->send >> on the condvar, ever. Worse,
 
90
there are no event watchers whatsoever. Basically, it creates a deadlock:
 
91
there is no way to make progress.
 
92
 
 
93
Some backends handle this by freezing, some by idling, and some do a 100%
 
94
cpu loop.
 
95
 
 
96
Since this program is nonsensical (and behaves as documented with all
 
97
backends, as AnyEvent makes no CPU tikme guarantees), this shouldn't be a
 
98
big deal - as soon as your program actually implements something sensible,
 
99
CPU usage will be normal.
 
100
 
 
101
=head1 Authors
 
102
 
 
103
Marc Lehmann <schmorp@schmorp.de>.
 
104