in reply to Re^3: How to deal with a forked proces that is waiting for user input?
in thread How to deal with a forked proces that is waiting for user input?

Here is my latest 2 implementations. They both allow me to detach the thread without a hang. Only the first one gets back the information even in the case of it waiting for user input. But I am not sure if either is exactly thread safe, and I am not sure I am using the thread queue as you intended. But this was the way I could get it working.

Implementation 1:

my $queue = new Thread::Queue; my $command = 'diskpart'; my $status = _timedCommand($command, 90); if ($status eq 'TIMEOUT') { print "\n=======TIMEOUT==========\n"; } my @ans; while (my $ref = $queue->dequeue_nb()) { if (ref($ref) =~ m/ARRAY/i) { push(@ans, @$ref); } else { push(@ans, $ref); } } print "@ans\n"; sub _timedCommand { my ($command, $time) = @_; my $pid :shared; my @tmp :shared; my $thr = async { $pid = open my $fh, "$command |" or die "$!"; push @tmp, $_ while <$fh>; $fh->close(); }; while ($thr->is_running() and $time > 0) { sleep(1); $time--; } if ($thr->is_joinable()) { $thr->detach(); $queue->enqueue(\@tmp); return 'OK'; } else { $thr->detach; kill 3, $pid; $queue->enqueue(\@tmp); return 'TIMEOUT'; } }

Implementation 2:

my $queue = new Thread::Queue; my $command = 'diskpart'; my $status = _timedCommand($command, 90); if ($status eq 'TIMEOUT') { print "\n=======TIMEOUT==========\n"; } my @ans; while (my $ref = $queue->dequeue_nb()) { if (ref($ref) =~ m/ARRAY/i) { push(@ans, @$ref); } else { push(@ans, $ref); } } print "@ans\n"; sub _timedCommand { my ($command, $time) = @_; my $pid :shared; my $thr = async { $pid = open my $fh, "$command |" or die "$!"; my @tmp :shared; push @tmp, $_ while <$fh>; $queue->enqueue(\@tmp); $fh->close(); }; while ($thr->is_running() and $time > 0) { sleep(1); $time--; } if ($thr->is_joinable()) { $thr->detach(); return 'OK'; } else { $thr->detach; kill 3, $pid; return 'TIMEOUT'; } }

In the second implementation I am seeing some wonky stuff when running the diskpart command, at times it @ans is populated and it prints out information, at other times it is not and doesn't. I have to assume its a timing issue, but I am not sure where it would be. For a dir command it works as expected.

Output from Implementation 1:

C:\Sandbox>perl ForkProcessTimeout.pl =======TIMEOUT========== Microsoft DiskPart version 5.1.3565 Copyright (C) 1999-2003 Microsoft Corporation. On computer: USITPAPADGD1C DISKPART>

Output from Implementation 2:

The output is inconsistent:

C:\Sandbox>perl ForkProcessTimeout.pl =======TIMEOUT========== Microsoft DiskPart version 5.1.3565 Copyright (C) 1999-2003 Microsoft Corporation. On computer: USITPAPADGD1C DISKPART> C:\Sandbox>perl ForkProcessTimeout.pl =======TIMEOUT========== C:\Sandbox>perl ForkProcessTimeout.pl =======TIMEOUT========== Microsoft DiskPart version 5.1.3565 Copyright (C) 1999-2003 Microsoft Corporation. On computer: USITPAPADGD1C DISKPART>

I think implementation 2 is the way I need to go for it to be threadsafe, but I don't get the consistency in output. Also when I run implementation 2 in the debugger it seems to consistently include the output.

Replies are listed 'Best First'.
Re^5: How to deal with a forked proces that is waiting for user input?
by BrowserUk (Patriarch) on Oct 22, 2008 at 16:12 UTC

    Would you please stop with the scatter gun approach!

    There are at least 4 different versions of your code, some in updated posts other two to a post, and it is impossible for me to keep up with them all.

    Please post one question at a time and at least wait for a reasonable amount of time for people to respond--remember they may be in different time zones to you, or just busy with their own stuff--before posting more.

    For the record. I've just tried 4 different versions of your code here and 3 of them 'work'...with minor changes.

    Like: don't use $fh->close; it will cause errors--which if you were using strict and warnings, one of them would tell you about. Use close( $fh ); instead.

    Could you please tell me which versions of threads & threads::shared you are using?


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Sorry about that, I just wanted to post my current status to show that I am not just waiting around for someone to do the work for me. I will be better about it.

      Versions: threads::shared version 1.12 threads version 1.63

      Incidentally I am using use strict; and use warnings; and I am not seeing anything about $fh->close(), not sure why it isn't

        Versions: threads::shared version 1.12 threads version 1.63

        Okay. That could explain some of the anomolies. I strongly recommend that you upgrade to the latest CPAN versions of both. There have been so many changes recently that it is impossible to keep track of what worked and what didn't with down-level versions.

        However, would you try this both before and after the upgrade and tell me how you get on (the stuff after the __END_ token is examples of it running on my system):

        #! perl -slw use strict; use threads qw[ yield ]; use threads::shared; $|++; my $cmd = $ARGV[ 0 ] || 'diskpart'; my $timeout = 10; my @results = timedCommand( $cmd, $timeout ); if( $results[0] ne '**TIMEOUT**' ) { print "Command returned\n", join '', @results; } else { print "Command timed out after $timeout seconds and returned\n", join '', @results; } sub timedCommand { my( $cmd, $timeout ) = @_; my $pid :shared; my( $thr ) = threads->create( \&pipedCmd, $cmd, $timeout, \$pid ); yield until $pid; sleep 1 while $pid and $timeout--; return $thr->join unless $pid; kill 3, $pid if $pid and $timeout; return '**TIMEOUT**', $thr->join; } sub pipedCmd { my( $cmd, $timeout, $pidref ) = @_; my @results; $$pidref = open my $fh, "$cmd |" or die "$!, $^E"; yield; push @results, $_ while defined( $_ = <$fh> ); undef $$pidref; return @results; } __END__ c:\test>timedCommand.pl Command timed out after 10 seconds and returned **TIMEOUT** Microsoft DiskPart version 5.1.3565 Copyright (C) 1999-2003 Microsoft Corporation. DISKPART> c:\test>timedCommand.pl exit Command returned Microsoft DiskPart version 5.1.3565 Copyright (C) 1999-2003 Microsoft Corporation. DISKPART> Leaving DiskPart... c:\test>timedCommand.pl cd Command returned c:\test

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.