Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Event Driven - timer

by perlthirst (Scribe)
on Dec 15, 2008 at 10:29 UTC ( #730385=perlquestion: print w/ replies, xml ) Need Help??
perlthirst has asked for the wisdom of the Perl Monks concerning the following question:

I have used Event module, to do a particular task for every n seconds.


my sample code is


use Event; $WatchObj = Event->timer('after' => 1, 'repeat' => 1, 'parked' => 1, 'cb' => sub { print "printing from with in sub routine \n"; } ); ## since used parked =1 , so called start explicitly. $WatchObj->start(); while (1) { }

For the above code, it should print "printing from with in the sub routine" for every 1 secs.


- But it is not printing.
- when calling $WatchObj->start() will it start and wait for the event occurs?
why it is not happening?
- If i used Event::loop instead of while(1){}. it is working properly.
- I want to register watcher, and my program has to continue with the next work. Kindly clarify.

Comment on Event Driven - timer
Download Code
Re: Event Driven - timer
by almut (Canon) on Dec 15, 2008 at 10:49 UTC
    If i used Event::loop instead of while(1){}. it is working properly.

    ...and that's your answer.  AFAIK, Event does not implement asynchronous events, which means that your code itself has to make sure that the routine one_event() is being called sufficiently often (which is what Event::loop() does, essentially).  In other words, there are no provisions made for interrupting loops like while(1){}.

    Maybe you could use more than one process or threads, and delegate the event 'polling' to one of them? (The details would depend on what data needs to be shared, etc.)

Re: Event Driven - timer
by ig (Vicar) on Dec 15, 2008 at 15:10 UTC

    You could use alarm and a signal handler.

    #!/usr/bin/perl # use strict; use warnings; use Data::Dumper; $SIG{ALRM} = sub { print "printing from within sub routine\n"; alarm(1); }; alarm(1); while(1) { print "main loop proceeding\n"; sleep(1); }

    produces:

    main loop proceeding printing from within sub routine main loop proceeding printing from within sub routine main loop proceeding printing from within sub routine main loop proceeding printing from within sub routine main loop proceeding printing from within sub routine main loop proceeding

    Note: you don't need the sleep in the main loop. You will, no doubt, have better things to do.

Re: Event Driven - timer
by zentara (Archbishop) on Dec 15, 2008 at 15:30 UTC
    almut gave you the answer, but she may have made it too technical.

    Your problem is commonly asked in GUI code programs, like Tk, Gtk2, Wx, etc. They are eventloop driven, and while(1){} statements interfere with the loop functioning. There are a few ways around it, some are hacks, some are good design.

    The best way around it is to use a thread, and it is mentioned in a section in "perldoc Event". Of course, if you use a thread, you may not need to even use Event.

    A common hack, is to put a statement in your while(1) block to refresh the eventloop on each pass thru, like:

    while(1){ ..... your code do_one_event_loop; # whatever Event syntax is ..... more of your code do_one_event_loop; # sprinkle them liberally }
    The drawback to this hack, is if your code is slow in sections, the loop won't update fast and you may miss prints.

    Another hack, is to somehow alter your while(1){} code, to fit into the Event style of programming. You usually can run things on a timer, so maybe you can rewrite your while(1){} to be run as a timer by Event.


    I'm not really a human, but I play one on earth Remember How Lucky You Are

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://730385]
Approved by svenXY
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2014-09-21 11:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (168 votes), past polls