perl - Get the value of a process executed in a child back to parent -
i'm looking solution allows me return values of process executed in child parent process. try have no idea hook return value:
use proc::processtable; use posix qw(:signal_h :errno_h :sys_wait_h); $sig{chld} = \&reaper; $count (1..10) { # start few demo childs if (fork () == 0) { &startchild; exit 0; } } { print "working\n"; sleep 1; } while (chkchildprocess()); sub startchild { print "starting child $$\n"; system("date"); #==>need output of "date" parent sleep 2 + rand 7; print "end child $$\n"; } sub chkchildprocess { $p (@{new proc::processtable->table}){ if ($p->ppid == $$){ $curpid{$$}=$p->pid; return 1; } } return undef; } sub reaper { $pid; $pid = waitpid(-1, &wnohang); if ($pid == -1) { # no child waiting. ignore it. } elsif (wifexited($?)) { print "process $pid exited.\n"; } else { print "false alarm on $pid.\n"; } $sig{chld} = \&reaper; # in case of unreliable signals }
any great.
the bg_eval
, bg_qx
methods of forks::super
made solve problem.
use forks::super 'bg_eval'; @result; $count (1 .. 10) { $result[$count] = bg_eval { $date = `date`; sleep 2 + rand 7; return $date; }; } print "$result[$_]\n" 1..10;
the block after bg_eval
run asynchronously in background process. when background process finished, variable $result[$count]
populated result.
when print $result[$_]
, 1 of 2 things happen. if background process associated variable finished, contain return value. if background process not finished, wait process finish, , make return value available in value.
Comments
Post a Comment