1 |
=head1 NAME |
2 |
|
3 |
AnyEvent::FAQ - frequently asked questions |
4 |
|
5 |
=head1 FAQs |
6 |
|
7 |
The newest version of this document can be found at |
8 |
L<http://pod.tst.eu/http://cvs.schmorp.de/AnyEvent/lib/AnyEvent/FAQ.pod>. |
9 |
|
10 |
=head2 My program exits before doing anything, what's going on? |
11 |
|
12 |
Programmers new to event-based programming often forget that you can |
13 |
actually do other stuff while "waiting" for an event to occur and |
14 |
therefore forget to actually wait when they do not, in fact, have anything |
15 |
else to do. |
16 |
|
17 |
Here is an example: |
18 |
|
19 |
use AnyEvent; |
20 |
|
21 |
my $timer = AnyEvent->timer (after => 5, cb => sub { say "hi" }); |
22 |
|
23 |
The expectation might be for the program to print "hi" after 5 seconds |
24 |
and then probably to exit. However, if you run this, your program will |
25 |
exit almost instantly: Creating the timer does not wait for it, instead |
26 |
the C<timer> method returns immediately and perl executes the rest of the |
27 |
program. But there is nothing left to execute, so perl exits. |
28 |
|
29 |
To force AnyEvent to wait for something, use a condvar: |
30 |
|
31 |
use AnyEvent; |
32 |
|
33 |
my $quit_program = AnyEvent->condvar; |
34 |
my $timer = AnyEvent->timer (after => 5, cb => sub { $quit_program->send }); |
35 |
|
36 |
$quit_program->recv; |
37 |
|
38 |
Here the program doesn't immediately exit, because it first waits for |
39 |
the "quit_program" condition. |
40 |
|
41 |
In most cases, your main program should call the event library "loop" |
42 |
function directly: |
43 |
|
44 |
use EV; |
45 |
use AnyEvent; |
46 |
|
47 |
... |
48 |
|
49 |
EV::loop; |
50 |
|
51 |
=head2 Why is my C<tcp_connect> callback never called? |
52 |
|
53 |
Tricky: C<tcp_connect> (and a few other functions in L<AnyEvent::Socket>) |
54 |
is critically sensitive to the caller context. |
55 |
|
56 |
In void context, it will just do its thing and eventually call the |
57 |
callback. In any other context, however, it will return a special "guard" |
58 |
object - when it is destroyed (e.g. when you don't store it but throw it |
59 |
away), tcp_connect will no longer try to connect or call any callbacks. |
60 |
|
61 |
Often this happens when the C<tcp_connect> call is at the end of a function: |
62 |
|
63 |
sub do_connect { |
64 |
tcp_connect "www.example.com", 80, sub { |
65 |
... lengthy code |
66 |
}; |
67 |
} |
68 |
|
69 |
Then the caller decides whether there is a void context or not. One can |
70 |
avoid these cases by explicitly returning nothing: |
71 |
|
72 |
sub do_connect { |
73 |
tcp_connect "www.example.com", 80, sub { |
74 |
... lengthy code |
75 |
}; |
76 |
|
77 |
() # return nothing |
78 |
} |
79 |
|
80 |
=head2 Why do some backends use a lot of CPU in C<< AE::cv->recv >>? |
81 |
|
82 |
Many people try out this simple program, or its equivalent: |
83 |
|
84 |
use AnyEvent; |
85 |
AnyEvent->condvar->recv; |
86 |
|
87 |
They are then shocked to see that this basically idles with the Perl |
88 |
backend, but uses 100% CPU with the EV backend, which is supposed to be |
89 |
sooo efficient. |
90 |
|
91 |
The key to understand this is to understand that the above program |
92 |
is actually I<buggy>: Nothing calls C<< ->send >> on the condvar, |
93 |
ever. Worse, there are no event watchers whatsoever. Basically, it creates |
94 |
a deadlock: there is no way to make progress, this program doesn't do |
95 |
anything useful, and this will not change in the future: it is already an |
96 |
ex-parrot. |
97 |
|
98 |
Some backends react to this by freezing, some by idling, and some do a |
99 |
100% CPU loop. |
100 |
|
101 |
Since this program is not useful (and behaves as documented with all |
102 |
backends, as AnyEvent makes no CPU time guarantees), this shouldn't be a |
103 |
big deal: as soon as your program actually implements I<something>, the |
104 |
CPU usage will be normal. |
105 |
|
106 |
=head2 Why does this FAQ not deal with L<AnyEvent::Handle> questions? |
107 |
|
108 |
Because L<AnyEvent::Handle> has a NONFAQ on its own that already deals |
109 |
with common issues. |
110 |
|
111 |
=head2 How can I combine L<Win32::GUI> applications with AnyEvent? |
112 |
|
113 |
Well, not in the same OS thread, that's for sure :) What you can do is |
114 |
create another ithread (or fork) and run AnyEvent inside that thread, or |
115 |
better yet, run all your GUI code in a second ithread. |
116 |
|
117 |
For example, you could load L<Win32::GUI> and L<AnyEvent::Util>, then |
118 |
create a portable socketpair for GUI->AnyEvent communication. |
119 |
|
120 |
Then fork/create a new ithread, in there, create a Window and send the C<< |
121 |
$WINDOW->{-Handle} >> to the AnyEvent ithread so it can C<PostMessage>. |
122 |
|
123 |
GUI to AnyEvent communication could work by pushing some data into a |
124 |
L<Thread::Queue> and writing a byte into the socket. The AnyEvent watcher |
125 |
on the other side will then look at the queue. |
126 |
|
127 |
AnyEvent to GUI communications can also use a L<Thread::Queue>, but to |
128 |
wake up the GUI thread, it would instead use C<< Win32::GUI::PostMessage |
129 |
$WINDOW, 1030, 0, "" >>, and the GUI thread would listen for these |
130 |
messages by using C<< $WINDOW->Hook (1030 (), sub { ... }) >>. |
131 |
|
132 |
=head2 My callback dies and... |
133 |
|
134 |
It must not - part of the contract betwene AnyEvent and user code is that |
135 |
callbacks do not throw exceptions (and don't do even more evil things, |
136 |
such as using C<last> outside a loop :). If your callback might die |
137 |
sometimes, you need to use C<eval>. |
138 |
|
139 |
If you want to track down such a case and you can reproduce it, you can |
140 |
enable wrapping (by calling C<< L<AnyEvent::Debug>::wrap >> or by setting |
141 |
C<PERL_ANYEVENT_DEBUG_WRAP=1> before starting your program). This will |
142 |
wrap every callback into an eval and will report any exception complete |
143 |
with a backtrace and some information about which watcher died, where it |
144 |
was created and so on. |
145 |
|
146 |
=head1 Author |
147 |
|
148 |
Marc Lehmann <schmorp@schmorp.de>. |
149 |
|