Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Run code every 6seconde

by vinian (Beadle)
on Dec 05, 2011 at 15:45 UTC ( #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! ...


Comment on Re: Run code every 6seconde
Select or Download Code
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 CardinalNumber (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
Node Status?
node history
Node Type: note [id://941880]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2014-07-14 10:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (257 votes), past polls