Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: 'better mousetrap': how to perform timed event

by tachyon (Chancellor)
on Apr 23, 2003 at 02:09 UTC ( #252428=note: print w/replies, xml ) Need Help??


in reply to 'better mousetrap': how to perform timed event

Why not just fork off a process to wait around and do the stuff?

# main loop while (1) { # do stuff # check for event my $event = check_for_event(); # now check for event and fork a child to deal with it # fork returns 0 to child, child PID to parent, undef if fails if ( $event and fork ==0 ) { # this is a child process sleep $whatever; do_stuff($event); # kid has done what was required so kill it exit 0; } # parent is continuing here, kid never gets here }

Obviously once check_for_event() has returned a true value for an event once you want to make sure it returns false for that event the next call or you will fork() your system to death. Depending on the frequency of events and the sleep time you can potentially have a steadily increasing burden of waiting kids build up.

Another option is to have a second parallel process to which you can send instructions. This process is in and endless loop waiting for instructions and executing them. You would probably pass it the instruction and an exec time which it stores in memory/file/DB and queries as required. Once an intruction has been processed you delete it.

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Replies are listed 'Best First'.
Re: Re: 'better mousetrap': how to perform timed event
by snafu (Chaplain) on Apr 23, 2003 at 06:35 UTC
    I thought of this approach but wasn't able to come up with a really good way for the parent to know when the child finished counting. I am guessing that I would perform something to find out when the child is reaped (supposing it exits when the timer is finished?).

    I apologize for some of the misunderstanding. This goes into somethings that have never been quite clear to me.

    _ _ _ _ _ _ _ _ _ _
    - Jim
    Insert clever comment here...

      You can find out exactly when the child exits but this is not what you specified which was exec something X seconds after Y event occurs. Forking a kid will do that for you and is quite portable. Why do you need to know when the child finishes? Not in spec. Kid will do its stuff X seconds after Y event. Why monitor it? What would you do if the child failed? If nothing redo Does it really matter? If no redo. What is failure? etc.....

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

        I don't need to exec() something as much as I need to call a sub-routine. I would figure that since the fork()'ed child is monitoring the time difference it probably couldn't call the sub-routine once the timelimit was reached, right? If I were using threads this could be possible, iiuc. However, forked children don't know what their parent is doing nor does the parent know what it's children are doing with the exception of when the child exits, right? Im not trying to be snide. I'm honestly asking the question because this is how I understand how it all works.

        So, in short, after 20 seconds once the vote has been initiated, I need to call end_vote() which does some things and then re-initializes all the variables used in the vote session.

        _ _ _ _ _ _ _ _ _ _
        - Jim
        Insert clever comment here...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://252428]
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: (2)
As of 2019-08-21 05:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?