1AnyEvent::FAQ(3) User Contributed Perl Documentation AnyEvent::FAQ(3)
2
3
4
6 AnyEvent::FAQ - frequently asked questions
7
8 The newest version of this document can be found at
9 <http://pod.tst.eu/http://cvs.schmorp.de/AnyEvent/lib/AnyEvent/FAQ.pod>.
10
11 My program exits before doing anything, what's going on?
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
15 anything 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 "timer" method returns immediately and perl executes the rest of
27 the 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 Why is my "tcp_connect" callback never called?
52 Tricky: "tcp_connect" (and a few other functions in AnyEvent::Socket)
53 is critically sensitive to the caller context.
54
55 In void context, it will just do its thing and eventually call the
56 callback. In any other context, however, it will return a special
57 "guard" object - when it is destroyed (e.g. when you don't store it but
58 throw it away), tcp_connect will no longer try to connect or call any
59 callbacks.
60
61 Often this happens when the "tcp_connect" call is at the end of a
62 function:
63
64 sub do_connect {
65 tcp_connect "www.example.com", 80, sub {
66 ... lengthy code
67 };
68 }
69
70 Then the caller decides whether there is a void context or not. One can
71 avoid these cases by explicitly returning nothing:
72
73 sub do_connect {
74 tcp_connect "www.example.com", 80, sub {
75 ... lengthy code
76 };
77
78 () # return nothing
79 }
80
81 Why do some backends use a lot of CPU in "AE::cv->recv"?
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 is
92 actually buggy: Nothing calls "->send" on the condvar, ever. Worse,
93 there are no event watchers whatsoever. Basically, it creates a
94 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
96 an 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
103 a big deal: as soon as your program actually implements something, the
104 CPU usage will be normal.
105
106 Why does this FAQ not deal with AnyEvent::Handle questions?
107 Because AnyEvent::Handle has a NONFAQ on its own that already deals
108 with common issues.
109
110 How can I combine Win32::GUI applications with AnyEvent?
111 Well, not in the same OS thread, that's for sure :) What you can do is
112 create another ithread (or fork) and run AnyEvent inside that thread,
113 or better yet, run all your GUI code in a second ithread.
114
115 For example, you could load Win32::GUI and AnyEvent::Util, then create
116 a portable socketpair for GUI->AnyEvent communication.
117
118 Then fork/create a new ithread, in there, create a Window and send the
119 "$WINDOW->{-Handle}" to the AnyEvent ithread so it can "PostMessage".
120
121 GUI to AnyEvent communication could work by pushing some data into a
122 Thread::Queue and writing a byte into the socket. The AnyEvent watcher
123 on the other side will then look at the queue.
124
125 AnyEvent to GUI communications can also use a Thread::Queue, but to
126 wake up the GUI thread, it would instead use "Win32::GUI::PostMessage
127 $WINDOW, 1030, 0, """, and the GUI thread would listen for these
128 messages by using "$WINDOW->Hook (1030 (), sub { ... })".
129
130 My callback dies and...
131 It must not - part of the contract betwene AnyEvent and user code is
132 that callbacks do not throw exceptions (and don't do even more evil
133 things, such as using "last" outside a loop :). If your callback might
134 die sometimes, you need to use "eval".
135
136 If you want to track down such a case and you can reproduce it, you can
137 enable wrapping (by calling "AnyEvent::Debug::wrap" or by setting
138 "PERL_ANYEVENT_DEBUG_WRAP=1" before starting your program). This will
139 wrap every callback into an eval and will report any exception complete
140 with a backtrace and some information about which watcher died, where
141 it was created and so on.
142
144 Marc Lehmann <schmorp@schmorp.de>.
145
146
147
148perl v5.28.0 2012-04-05 AnyEvent::FAQ(3)