Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: AnyEvents - mainly

by Corion (Patriarch)
on Apr 26, 2020 at 06:17 UTC ( [id://11116070]=note: print w/replies, xml ) Need Help??


in reply to AnyEvents - mainly

You don't even need your own mainloop, as AnyEvent brings its own main loop with it.

I would rewrite your main loop as follows:

my $loop = AnyEvent->condvar; my $a = 0; sub heartbeat { open( $fh, '>>', $msg_log ); print $fh "looping $a\n"; close $fh; }; AnyEvent->timer( interval => 5, cb => \&heartbeat ); $loop->recv; exit 0;

I would move reacting to IO out of the main loop as well:

$iocv->cb( \&on_io ); sub on_io { open( $fh, '>>', $msg_log ); print $fh "loop data: $data\n"; close $fh; undef( $iocv ); $iocv = AnyEvent->condvar; $iocv->cb( \&on_io ); };

Ideally, you don't have your own main loop and only have callbacks and let AnyEvent decide, which callback to run, depending on what kind of event has arrived.

Replies are listed 'Best First'.
Re^2: AnyEvents - mainly
by anita2R (Scribe) on Apr 26, 2020 at 10:47 UTC

    Corion,

    Thanks for that. I didn't appreciate that AnyEvents provides its own main loop.

    I will rewrite it as suggested

    anita2R

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11116070]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-19 06:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found