Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Run code every 6seconde

by vinian (Beadle)
on Dec 05, 2011 at 15:45 UTC ( [id://941880]=note: print w/replies, xml ) Need Help??


in reply to Run code every 6seconde

it's easy to do it with POE

#!/usr/bin/perl # vim: set et sta ts=4 sw=4 sts=4: # vi: set ts=4: # # use strict; use warnings; use POE; POE::Session->create( inline_states => { _start => \&handler_start, fun1 => \&fun1, fun2 => \&fun2, _stop => \&handler_stop, }); POE::Kernel->run(); exit; sub handler_start { my ($kernel, $heap, $session) = @_[KERNEL, HEAP, SESSION]; print "Starting !\n"; $kernel->yield( 'fun1' ); $kernel->yield( 'fun2' ); } sub fun1 { my ($kernel, $heap, $session) = @_[KERNEL, HEAP, SESSION]; print "I'm function 1!\n"; $kernel->delay(fun1 => 6); } sub fun2 { my ($kernel, $heap, $session) = @_[KERNEL, HEAP, SESSION]; print "I'm FUNCTION 2!\n"; $kernel->delay(fun2 => 6); } sub handler_stop { print "Stop !!\n"; }

Output:

Starting ! I'm function 1! I'm FUNCTION 2! I'm function 1! I'm FUNCTION 2! I'm function 1! I'm FUNCTION 2! ...

Replies are listed 'Best First'.
Re^2: Run code every 6seconde
by Anonymous Monk on Dec 05, 2011 at 16:11 UTC
    You call that easy? Holy camel shit, is POE ridiculously verbose. Try this instead:
    #!/usr/bin/perl use 5.010; use strictures; use AnyEvent qw(); my %events; $events{function1} = AE::timer(0, 6, sub { say q(I'm function 1!); }); $events{function2} = AE::timer(0, 6, sub { say q(I'm function 2!); }); $events{main} = AE::cv->recv;
Re^2: Run code every 6seconde
by SankoR (Prior) on Dec 05, 2011 at 20:32 UTC
    Whew! This is a great example of why I never used POE. Fortunately, Reflex hides most of… that:
    use 5.010; use Reflex::Interval; my %events; $events{func1} = Reflex::Interval->new(interval => 1, on_tick => sub { say 'I am function 1!' } ); $events{func2} = Reflex::Interval->new(interval => 1, on_tick => sub { say 'I am function 2!' } ); Reflex->run_all;
    Edit: ++ for the AnyEvent example above too.

Log In?
Username:
Password:

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

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

    No recent polls found