Re: how to run a command-line program from Perl/Tk (ExecuteCommand?)



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;
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)
&&( $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
.



Relevant Pages

  • Control a process interactively (pexpect) and save data
    ... The main goal is to record all sequence of commands and responses in order to automatically generate pexpect script. ... The optional input argument should be a string to be sent to the child process, or None, if no data should be sent to the child." ... So it "waits for process to terminate" and I'm unable to interact... ... If I enter 'exit' sh exits, ...
    (comp.lang.python)
  • Re: How to exit a shell script
    ... not exit the script? ...     then ... script continue to exit without error. ... not executed), but it doesn't terminate the script, whereas the "exit 0" ...
    (comp.unix.shell)
  • Re: How to exit a shell script
    ... not exit the script? ... script continue to exit without error. ... echo $$ Before exit 1 ... not executed), but it doesn't terminate the script, whereas the "exit 0" ...
    (comp.unix.shell)
  • Re: aborting a bash script with control-c
    ... A couple of signals can be generated from the keyboard, ... but by default sends the "request to terminate" signal 15. ... shell script specifies what should happen when various signals are received. ... trap 'exit' INT TERM # exit when interrupted or terminated ...
    (comp.unix.shell)
  • Re: How can I exit from BEGIN?
    ... I have a BEGIN section that does some checks first then needs to exit the ... script if they fail. ... a way of forcing the script to terminate in BEGIN? ...
    (comp.lang.awk)