bloonix has asked for the wisdom of the Perl Monks concerning the following question:
Hello Monks,
I'm seeking for you wisdom and hope that someone can help me a bit.
I wrote a little daemon that executes programs with IPC::Open3 and intercepts the output from stdout and stderr. IO::Select is used to check if a handle is ready and alarm() for timeouts within eval{}.
The daemon is running very nice on linux, but unfortunately not on windows.
Now I was searching for another solution and thought about to use threads.
In the following example a thread is created to execute a program. The parent waits until $timeout and then detach the thread and kills the process that is maybe still running.
I would be very pleased for your wisdom.
Cheers
Jonny
Edit: a short code fix
I'm seeking for you wisdom and hope that someone can help me a bit.
I wrote a little daemon that executes programs with IPC::Open3 and intercepts the output from stdout and stderr. IO::Select is used to check if a handle is ready and alarm() for timeouts within eval{}.
The daemon is running very nice on linux, but unfortunately not on windows.
Now I was searching for another solution and thought about to use threads.
In the following example a thread is created to execute a program. The parent waits until $timeout and then detach the thread and kills the process that is maybe still running.
Now my question to you is: could it be simplier? I don't know if my code example is too much dirty, because I haven't the finest idea of windows.#!perl.exe use strict; use warnings; use threads; use threads::shared; use Data::Dumper; use IPC::Open3; use Symbol; my %data : shared; my $timeout = 3; my $command = "perl test.pl"; my $tid = threads->create(sub { execute($command) }); while (--$timeout) { if ($tid->is_joinable) { $tid->join; last; } sleep 1; } if ($timeout == 0) { if ($data{pid}) { kill -9, $data{pid}; } $tid->detach; $data{timedout} = 1; } print Dumper(\%data); sub execute { my $in = Symbol::gensym(); my $out = Symbol::gensym(); my $err = Symbol::gensym(); my $pid = open3($in, $out, $err, @_); close $in; $data{pid} = $pid; # sorry for that dirty handling of $out and $err :-) # it was just a hack for tests $data{stdout} = do { local $/; <$out> }; $data{stderr} = do { local $/; <$err> }; close $out; close $err; threads->exit; }
I would be very pleased for your wisdom.
Cheers
Jonny
Edit: a short code fix
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Windows, threads and IPC::Open3
by BrowserUk (Patriarch) on Nov 07, 2010 at 22:18 UTC | |
Re: Windows, threads and IPC::Open3
by ikegami (Patriarch) on Nov 07, 2010 at 21:01 UTC | |
by bloonix (Monk) on Nov 07, 2010 at 22:53 UTC | |
by ikegami (Patriarch) on Nov 08, 2010 at 01:56 UTC | |
by ikegami (Patriarch) on Nov 08, 2010 at 01:57 UTC |
Back to
Seekers of Perl Wisdom