Re: how to run a command-line program from Perl/Tk (ExecuteCommand?)
- From: zentara <zentara@xxxxxxxxxxxxxx>
- Date: Sat, 01 Dec 2007 14:26:09 GMT
On 30 Nov 2007 23:05:27 GMT, Will Parsons <oudeis@xxxxxxxxxxxxxxxx>
wrote:
zentara wrote:
"A thread exited while 2 threads were running."
It's harmless, and means that the program exited without the threads^^^^^^^^^^^^^^^^^^^^^^
being joined. (unless it was detached).
I _am_ detaching the thread, so I cannot join it explicitly, if I am
understanding the documentation correctly.
For a thread to be joined, it must reach the end of it's code block, OR
you can issue a return.
Well when it's detached, the thread still needs to return (or reach
end_of_code_block), or the error message will be generated, if the main
thread exits.
Another thing to watch for, is using 'exit' in a thread, will exit all
threads and terminate the application.
Depending on how your thread code is written,
it may not be possible to tell it to return (it may be running something
that you cannot put into a loop that checks for a shared variable).
It turns out that it's easy to tell it to return via another shared
variable. I now have something like:
require threads;
require threads::shared;
require Thread::Queue;
import threads::shared;
$Q = new Thread::Queue;
share(\$Script);
share(\$Terminate);
sub run_script {
$| = 1;
until ($Terminate) {
if ($Script) {
open PROC, "perl $Script 2>&1 |" or die "...";
while (sysread(PROC, my $buffer, 1024) > 0) {
$Q->enqueue($buffer);
}
close PROC;
$Script = undef;
} else {
sleep 1;
}
}
}
threads->new(\&run_script)->detach;
and an exit handler:
sub exit_application {
$Terminate = 1;
sleep 1;
exit;
}
This seems to work fine, at least as long as the script is not running
when the application is terminated. Probably putting a check for
$Terminate into the while(sysread...) loop would make it act nicely
even if the script is being executed when the program is terminated,
but I'll have to do a bit of experimentation.
I would try to put into the while test too, something like
$Q = new Thread::Queue;#> until ($Terminate) {
share(\$Script);
share(\$Terminate);
sub run_script {
$| = 1;
if ($Script) {
open PROC, "perl $Script 2>&1 |" or die "...";
while ((sysread(PROC, my $buffer, 1024) > 0)
&&( $Terminate == 0 )) {
.....
......
}
threads->new(\&run_script)->detach;
Your help in figuring out how to use threads has been invaluable.
Thank you.
- Will
No problem. Just pass it on to the next guy who needs to know. :-)
zentara
--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
.
- References:
- Re: how to run a command-line program from Perl/Tk (ExecuteCommand?)
- From: Will Parsons
- Re: how to run a command-line program from Perl/Tk (ExecuteCommand?)
- Prev by Date: Tabindex binding with text widget
- Next by Date: Re: Tabindex binding with text widget
- Previous by thread: Re: how to run a command-line program from Perl/Tk (ExecuteCommand?)
- Next by thread: Tabindex binding with text widget
- Index(es):
Relevant Pages
|