Re: Event examples || documentation
[email protected] (Joshua N Pritikin)
| Newsgroups | perl.loop |
|---|---|
| Message-ID | <[email protected]> |
On Mon, Oct 16, 2000 at 03:28:42PM -0500, [email protected] wrote: > I am spending most of my time deciphering the terminology and > corresponding function calls. > > Here is my project: > > Create a server deamon that waits for a client to connect and > submit configuration data to fire off a process. There is a limited > number of resources, so queueing and resource management is > a must. Once queueing and resource management is handled, > a job is forked off and the system waits for the child to die to > know that those resources are now available. > > The problem lies in that perl's sigchld handling isn't reliable, due > to the unavailablity of asyncronous non-blocking I/O. Event.pm > supposedly can take care of this. Yes, use POSIX ":sys_wait_h"; Event->signal(signal => 'CHLD', cb => sub { my ($e) = @_; # warn "CHLD ".$e->hits; for (my $h=0; $h < $e->hits; $h++) { my $died = waitpid(-1,&WNOHANG); # process it } }); > My understanding so far- > > Start an Event loop w/ a watcher and given parameters and when > a change to the filehandle occurs, the callback is invoked. The > main routine can interact w/ the Event loop w/ varies functions > such as: unloop/sweep, etc. > > I don't know what Event thingies I should do in this situation. I'll > gladly write-up an example chapter once I get it figured out. > > I think my code will look something like: > > use Event qw(loop, unloop) Remove comma. > $server = IO::Socket::INET->new ( blah) Yes, see NetServer::Portal::Top for a code example. > $w=Event->io(fd=>\$server, poll=>'r' , cb=\&handle_client) > loop; > > $sig_watch=Event->signal(signal=>'chld', async=>1, cb=\&chld_mangler) Why async=>1? > loop; Only loop() once. ;-) > sub child_mangler > { > while(something) > { > $pid=waitpid(-1, non-blocking-option) > delete_pid_from_queue($pid) > num_kids--; > } > } Yup. > sub handle_client > { > read config info; > resource_manager; > create_process; > } > sub create_process; > { > if # of kids <= $max_num_kids > { > add_pid_to_queue > fork; # do job > } > } > > I didn't include some of the other sub routines, but this is the main idea. > > > Am I heading in the right direction? Yes. :-) -- Never ascribe to malice that which can be explained by stupidity. (via, but not speaking for Deutsche Bank)