Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Suggestions on differentiating child processes

by tmoertel (Chaplain)
on Nov 12, 2004 at 17:52 UTC ( [id://407444]=note: print w/replies, xml ) Need Help??


in reply to Suggestions on differentiating child processes

Have each child write its status to a pid-named file in a status directory. When its work is complete, have it delete its file. If you use File::Temp, the cleanup is automatic.
use File::Temp; use constant STATUS_DIR => '/tmp/status-dir'; { my $fh; # hidden from outer code # status is logged only when you have created # the status dir by hand; this lets you turn off # logging by deleting the status dir if ( -d STATUS_DIR ) { $fh = new File::Temp( template => "$$-XXXXXX", dir => STATUS_DIR, suffix => ".txt", unlink => 1 ) or die "can't open temp file: $!"; $fh->autoflush(1); } sub log_status { print $fh "$$ - ", scalar localtime, " - ", @_, "\n" if $fh; } sub close_status { $fh->close if $fh; } }
Then your work code can log its progress:
# here is the meat of our worker code log_status("worker starting; config=blah..."); # ... work ... log_status("worker starting stage two"); # ... more work ... log_status("worker starting stage three"); sleep 30; # it's a long one # finally! whew, that was hard work :) log_status("worker exiting"); close_status();
If a worker bee gets hung up, you can observe its status from the log that corresponds to its pid. For example, I justed fired one up with pid 14416. Let's see what it is doing:
$ cat /tmp/status-dir/14416-UXL0uq.txt 14416 - Fri Nov 12 12:40:06 2004 - worker starting; config=blah... 14416 - Fri Nov 12 12:40:06 2004 - worker starting stage two 14416 - Fri Nov 12 12:40:06 2004 - worker starting stage three
Ah, it's that darn stage-three work! It always takes too long.

Also, since the log files are automatically deleted upon completion, you can see what tasks are still running by listing the contents of the status-log directory.

Cheers,
Tom

Replies are listed 'Best First'.
Re^2: Suggestions on differentiating child processes
by gnu@perl (Pilgrim) on Nov 12, 2004 at 18:04 UTC
    Very good idea as well, thank you. This might work quite well. I'll have to give it a go. Thanks much. Chad.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-03-29 12:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found