watching worker time processes

Marc Lehmann schmorp at schmorp.de
Fri Mar 13 08:00:35 CET 2015


On Wed, Mar 11, 2015 at 03:22:16PM +0100, rahed <raherh at gmail.com> wrote:
> AnyEvent::Fork::Pool. Each process must take maximum time to
> execute. I'm new to event programming, normally I would watch for ALRM
> signal in eval with alarm function set for maximum time to elapse.

So you want to enforce a limit on cpu time.

I don't think there is a good way to to this for perl, as perl doesn't
interrupt opcodes because of a signal, so in general, you might never
catch the signal.

What would work reliably is to let the OS kill your process afteer a while.
SIGALRM without a signal handler will achieve that, but AnyEvent::Fork::RPC
will consider this an error.

If all you want is some subprocess and time-limit it, without passing data
(as in your example), you should be able to do this with AnyEvent::Fork
alone, i.e. something like this (your code kept the >).


   > use AnyEvent;
   > use AnyEvent::Fork;
   > 
   > $done = AE::cv;
   > $fork = AnyEvent::Fork
   >   ->new
   >   ->require ("AnyEvent::Fork::RPC::Async")
       ->eval (do { local $/; <DATA> })
       ->run ("myworker", sub {
          my ($fh) = @_;

          # now wait for activity on the fh, either an "ok" byte or EOF
          my $io; $io = AE::io $fh, 0, sub {
             my $len = sysread $fh, my $status, 1;

             if ($len > 0) {
                $done->("all ok");
             } elsif (defined $len) {
                $done->("timeout");
             } else {
               # check for EWOULDBLOCK or other errors etc.
             }

          };

     my $status = $done->recv;

   > __DATA__
   > 
   > use AnyEvent;
   > 
   > sub myworker {
       my ($slave_fh) = @_;

       alarm 10;

   > # ... time consuming code gathering $data, generally under timer period

       # signal success
       syswrite $slave_fh, "\001";
   > }

The idea is to use the filehandle provided by AnyEvent::Fork to signal
a success byte at the end - if the worker is killed (and hasn't forked
itself etc.), then the handle will simply be closed by the OS, resulting
in an EOF and a missing success byte.

-- 
                The choice of a       Deliantra, the free code+content MORPG
      -----==-     _GNU_              http://www.deliantra.net
      ----==-- _       generation
      ---==---(_)__  __ ____  __      Marc Lehmann
      --==---/ / _ \/ // /\ \/ /      schmorp at schmorp.de
      -=====/_/_//_/\_,_/ /_/\_\



More information about the anyevent mailing list