Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^4: Win32 capturing output from a process that may hang

by Random_Walk (Parson)
on Mar 02, 2005 at 18:54 UTC ( #435967=note: print w/ replies, xml ) Need Help??


in reply to Re^3: Win32 capturing output from a process that may hang (Updated!)
in thread Win32 capturing output from a process that may hang

Hi BrowserUK

Well you can put your jaundiced view aside for a second here. Your code did not do exactly what I wanted (I caught the wait timeout even if code is done issue) but I have altered it and as far as my testing goes up to now I think I have exactly what I want. Here are the salient fragments

# this is a direct c&p from a larger opus of code # but you should get the idea. # command is worked out earlier depending on the # platform we are running on my $timeout=60; my @sqlo; my $Q = new Thread::Queue; threads->create( \&RunInThread, $command, $Q ); my $pid = $Q->dequeue; for (1..$timeout) { sleep 1; my $result=$Q->dequeue_nb; # non_block, return undef if nowt o +n queue next unless $result; # twas nowt on queue if ($result eq "$pid is done") { $trace->trace("SQL process finished within $_ seconds"); last; } push @sqlo, $result; } my $killed = kill 9, $pid if kill 0, $pid; # kill child if still +alive if ($killed) { $trace->trace("Had to kill SQL process, taking more than $time +out secs"); } sub RunInThread { my ( $cmd, $Q) = @_; my $pid = open CMD, "$cmd |" or $trace->die("$cmd : $!"); $Q->enqueue($pid); $Q->enqueue( $_ ) while defined( $_ = <CMD> ); $Q->enqueue("$pid is done"); }

The multithreading is also great as I may have several DBs to examine so a little refactoring and I can do them all in parallel improving the chance of completion within the 120 sec time limit no end.

Cheers,
R.

Pereant, qui ante nos nostra dixerunt!


Comment on Re^4: Win32 capturing output from a process that may hang
Download Code
Replies are listed 'Best First'.
Re^5: Win32 capturing output from a process that may hang (Updated!)
by BrowserUk (Pope) on Mar 02, 2005 at 19:58 UTC

    Neat!++

    Sending the pid back to the parent thread and having it do the killing is very shrewd--and obvious. Now you've shown me:)

    Thanks for the feedback. I love to learn.


    Examine what is said, not who speaks.
    Silence betokens consent.
    Love the truth but pardon error.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (19)
As of 2015-07-30 13:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (271 votes), past polls