Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: creative forking (code)

by Fastolfe (Vicar)
on Nov 04, 2001 at 03:09 UTC ( [id://123106]=note: print w/replies, xml ) Need Help??


in reply to creative forking (code)

Generally if I have a monolithic process that I'm using to watch for events on a filehandle *and* handle timed events simultaneously, I'll just build that into the process's event loop:
sub event_loop { my $next_event = &get_next_event; # e.g. time() + 5 if (select($rh, $wh, $eh, $next_event - time())) { &process($rh, $wh, $eh); } elsif ($next_event <= time()) { &do_timed_events; } } &event_loop while $running;
If you're using a forking model, you may need to provide some additional information. Is each child process dedicated to handling one specific timed event, or do you have an existing pool of forked processes doing their own thing, and you just don't care if one of them handles the timed event?

If you have one or more children that are guaranteed to be running, just assign out your timed event to the first child:

if (fork()) { $children_spawned++; return 1; } else { # Here, the *very first* child won't have $children_spawned unless ($children_spawned) { print "I'm the first!\n"; &do_main_with_timed_event_processing; } else { &do_main_without_timed_events; } }
If that first process isn't guaranteed to be running, you may just need some inter-process communication between your children. Whoever steps up to the plate first to handle a timed event gets it. This can be done with shared memory or semaphores. See perlipc for more information.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2024-04-24 15:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found