.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.40) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "AnyEvent::FAQ 3pm" .TH AnyEvent::FAQ 3pm "2020-11-09" "perl v5.32.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" AnyEvent::FAQ \- frequently asked questions .SH "FAQs" .IX Header "FAQs" The newest version of this document can be found at . .SS "My program exits before doing anything, what's going on?" .IX Subsection "My program exits before doing anything, what's going on?" Programmers new to event-based programming often forget that you can actually do other stuff while \*(L"waiting\*(R" for an event to occur and therefore forget to actually wait when they do not, in fact, have anything else to do. .PP Here is an example: .PP .Vb 1 \& use AnyEvent; \& \& my $timer = AnyEvent\->timer (after => 5, cb => sub { say "hi" }); .Ve .PP The expectation might be for the program to print \*(L"hi\*(R" after 5 seconds and then probably to exit. However, if you run this, your program will exit almost instantly: Creating the timer does not wait for it, instead the \f(CW\*(C`timer\*(C'\fR method returns immediately and perl executes the rest of the program. But there is nothing left to execute, so perl exits. .PP To force AnyEvent to wait for something, use a condvar: .PP .Vb 1 \& use AnyEvent; \& \& my $quit_program = AnyEvent\->condvar; \& my $timer = AnyEvent\->timer (after => 5, cb => sub { $quit_program\->send }); \& \& $quit_program\->recv; .Ve .PP Here the program doesn't immediately exit, because it first waits for the \*(L"quit_program\*(R" condition. .PP In most cases, your main program should call the event library \*(L"loop\*(R" function directly: .PP .Vb 2 \& use EV; \& use AnyEvent; \& \& ... \& \& EV::loop; .Ve .ie n .SS "Why is my ""tcp_connect"" callback never called?" .el .SS "Why is my \f(CWtcp_connect\fP callback never called?" .IX Subsection "Why is my tcp_connect callback never called?" Tricky: \f(CW\*(C`tcp_connect\*(C'\fR (and a few other functions in AnyEvent::Socket) is critically sensitive to the caller context. .PP In void context, it will just do its thing and eventually call the callback. In any other context, however, it will return a special \*(L"guard\*(R" object \- when it is destroyed (e.g. when you don't store it but throw it away), tcp_connect will no longer try to connect or call any callbacks. .PP Often this happens when the \f(CW\*(C`tcp_connect\*(C'\fR call is at the end of a function: .PP .Vb 5 \& sub do_connect { \& tcp_connect "www.example.com", 80, sub { \& ... lengthy code \& }; \& } .Ve .PP Then the caller decides whether there is a void context or not. One can avoid these cases by explicitly returning nothing: .PP .Vb 4 \& sub do_connect { \& tcp_connect "www.example.com", 80, sub { \& ... lengthy code \& }; \& \& () # return nothing \& } .Ve .ie n .SS "Why do some backends use a lot of \s-1CPU\s0 in ""AE::cv\->recv""?" .el .SS "Why do some backends use a lot of \s-1CPU\s0 in \f(CWAE::cv\->recv\fP?" .IX Subsection "Why do some backends use a lot of CPU in AE::cv->recv?" Many people try out this simple program, or its equivalent: .PP .Vb 2 \& use AnyEvent; \& AnyEvent\->condvar\->recv; .Ve .PP They are then shocked to see that this basically idles with the Perl backend, but uses 100% \s-1CPU\s0 with the \s-1EV\s0 backend, which is supposed to be sooo efficient. .PP The key to understand this is to understand that the above program is actually \fIbuggy\fR: Nothing calls \f(CW\*(C`\->send\*(C'\fR on the condvar, ever. Worse, there are no event watchers whatsoever. Basically, it creates a deadlock: there is no way to make progress, this program doesn't do anything useful, and this will not change in the future: it is already an ex-parrot. .PP Some backends react to this by freezing, some by idling, and some do a 100% \s-1CPU\s0 loop. .PP Since this program is not useful (and behaves as documented with all backends, as AnyEvent makes no \s-1CPU\s0 time guarantees), this shouldn't be a big deal: as soon as your program actually implements \fIsomething\fR, the \&\s-1CPU\s0 usage will be normal. .SS "Why does this \s-1FAQ\s0 not deal with AnyEvent::Handle questions?" .IX Subsection "Why does this FAQ not deal with AnyEvent::Handle questions?" Because AnyEvent::Handle has a \s-1NONFAQ\s0 on its own that already deals with common issues. .SS "How can I combine Win32::GUI applications with AnyEvent?" .IX Subsection "How can I combine Win32::GUI applications with AnyEvent?" Well, not in the same \s-1OS\s0 thread, that's for sure :) What you can do is create another ithread (or fork) and run AnyEvent inside that thread, or better yet, run all your \s-1GUI\s0 code in a second ithread. .PP For example, you could load Win32::GUI and AnyEvent::Util, then create a portable socketpair for \s-1GUI\-\s0>AnyEvent communication. .PP Then fork/create a new ithread, in there, create a Window and send the \f(CW\*(C`$WINDOW\->{\-Handle}\*(C'\fR to the AnyEvent ithread so it can \f(CW\*(C`PostMessage\*(C'\fR. .PP \&\s-1GUI\s0 to AnyEvent communication could work by pushing some data into a Thread::Queue and writing a byte into the socket. The AnyEvent watcher on the other side will then look at the queue. .PP AnyEvent to \s-1GUI\s0 communications can also use a Thread::Queue, but to wake up the \s-1GUI\s0 thread, it would instead use \f(CW\*(C`Win32::GUI::PostMessage $WINDOW, 1030, 0, ""\*(C'\fR, and the \s-1GUI\s0 thread would listen for these messages by using \f(CW\*(C`$WINDOW\->Hook (1030 (), sub { ... })\*(C'\fR. .SS "My callback dies and..." .IX Subsection "My callback dies and..." It must not \- part of the contract betwene AnyEvent and user code is that callbacks do not throw exceptions (and don't do even more evil things, such as using \f(CW\*(C`last\*(C'\fR outside a loop :). If your callback might die sometimes, you need to use \f(CW\*(C`eval\*(C'\fR. .PP If you want to track down such a case and you can reproduce it, you can enable wrapping (by calling \f(CW\*(C`AnyEvent::Debug::wrap\*(C'\fR or by setting \&\f(CW\*(C`PERL_ANYEVENT_DEBUG_WRAP=1\*(C'\fR before starting your program). This will wrap every callback into an eval and will report any exception complete with a backtrace and some information about which watcher died, where it was created and so on. .SH "Author" .IX Header "Author" Marc Lehmann .