Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Killing threads in perl

Reply
Thread Tools

Killing threads in perl

 
 
joergwenzel@gmx.de
Guest
Posts: n/a
 
      03-07-2007
Hi,

who can help me, to kill the threads in this script.
I create lot of thread with a tar. If the tar canceled with errors, i
have to kill the rest of
the threads with tar. I dont now what is the best way. I tested with
kill Hup, but doesn't work.

code:
..
# fill my array with
.....
my $cmd =
"GZIP=$gzip; $tar -C $startdir -X $tapedir/exclude -czf
$tapedir/$dname"
. ".tar.gz . ";
push( @restore, "$tar -C (Please set path here!) -xzvf
$dname.tar.gz" );
push( @todo, $cmd ); # push Task on TODO
....
..
while ( $#todo >= 0 && $result == 0) {
last if($result > 0);
my $cmd = pop(@todo) ;
$sem->down(); # only n threads on the same time
if ( $result == 0) {
my $thread = threads->new(
\&Task,
$cmd, # create a new thread
);
$thread->detach();
}
....
..

#-------------------------------------------------------------------------
sub Task {
my (
$cmd, # Parameter
) = @_;
my $id = threads->self->tid;
$thread_run++;
my $result += system ($cmd)/256;;
if ($debug == 1) { print " Return: $result \n\n"; }
$sem->up();
#$thread_run--;
printf "Thread %02d: fertig.\n", $id;
kill ("HUP", -$$);
if ($result >0){print "\n\n Fehler !!!!!!!!\n\n";
kill ("HUP", -$$);

}

}

 
Reply With Quote
 
 
 
 
gf
Guest
Posts: n/a
 
      03-07-2007
On Mar 7, 7:44 am, joergwen...@gmx.de wrote:
> Hi,
>
> who can help me, to kill the threads in this script.
> I create lot of thread with a tar. If the tar canceled with errors, i
> have to kill the rest of
> the threads with tar. I dont now what is the best way. I tested with
> kill Hup, but doesn't work.
>

Off the top of my head I'd say that you want to use fork() instead of
threads if you want to be able to kill the tar processes.

When I used threads I had multiple LWP sessions running and each
thread looped until a queued list of URLs was processed, then they'd
exit automatically. Your tar processes don't have the intelligence to
know when to quit, which is why you're in that corner.

perldoc -f fork

Also read through Perlfaq 8

 
Reply With Quote
 
 
 
 
Eric Schwartz
Guest
Posts: n/a
 
      03-07-2007
A brief comment on a matter unrelated to your direct question:

writes:
> while ( $#todo >= 0 && $result == 0) {
> last if($result > 0);
> my $cmd = pop(@todo) ;


....

}

This is a more Perl-ish way to do that loop:

foreach my $cmd (@todo) {
last if $result > 0;

# do stuff with $cmd as per usual.
}

Note that you don't have to check $result in the condition, since you
do so in the loop. You only need to check it one place, and doing it
in the loop makes the condition look cleaner. And using $#array in a
conditional looks kinda wonky, and is unnecessary most of the time
(I'd say "all of the time", but then somebody would come along and
find the .0001% case where it matters .

-=Eric
 
Reply With Quote
 
zentara
Guest
Posts: n/a
 
      03-08-2007
On 7 Mar 2007 06:44:56 -0800, wrote:

>who can help me, to kill the threads in this script.
>I create lot of thread with a tar. If the tar canceled with errors, i
>have to kill the rest of
>the threads with tar. I dont now what is the best way. I tested with
>kill Hup, but doesn't work.


In order for a Perl thread to be killed individually, you need
to make it go to the end of it's code block, or do a return.

So, you need a shared variable in the thread
code, to tell it to return.

Like:

my $die:shared;
$die =0;

#in thread code check for it:
if($die){return}

If you don't detach, you still need to return from the thread
block, before a join can happen.

Joining is preferred over detaching, because you can reuse
the thread, or it's space, and that will avoid gaining memory as
you constantly spawn threads.

In c, threads work alot cleaner, but in Perl, you need to watch
for memory accumulating, and that means reusing threads.

zentara


--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
 
Reply With Quote
 
joergwenzel@gmx.de
Guest
Posts: n/a
 
      03-09-2007

> Like:
>
> my $die:shared;
> $die =0;
>
> #in thread code check for it:
> if($die){return}
>
> If you don't detach, you still need to return from the thread
> block, before a join can happen.
>
> Joining is preferred over detaching, because you can reuse
> the thread, or it's space, and that will avoid gaining memory as
> you constantly spawn threads.


Yes, but my tar still running at the thread? My $return come back
at the end of the tar. the next line of the script start behind the
tar
no "if" help me at this time.

threads:

1. running tar 50min finished ok
2. running tar 30min breakup error
3. running tar 90min running <-- to kill
4. running tar 120min running <-- to kill



 
Reply With Quote
 
zentara
Guest
Posts: n/a
 
      03-09-2007
On 9 Mar 2007 00:58:53 -0800, wrote:

>
>> Like:
>>
>> my $die:shared;
>> $die =0;
>>
>> #in thread code check for it:
>> if($die){return}
>>
>> If you don't detach, you still need to return from the thread
>> block, before a join can happen.
>>
>> Joining is preferred over detaching, because you can reuse
>> the thread, or it's space, and that will avoid gaining memory as
>> you constantly spawn threads.

>
>Yes, but my tar still running at the thread? My $return come back
>at the end of the tar. the next line of the script start behind the
>tar
>no "if" help me at this time.
>
>threads:
>
>1. running tar 50min finished ok
>2. running tar 30min breakup error
>3. running tar 90min running <-- to kill
>4. running tar 120min running <-- to kill
>


Without seeing your thread code, you have 2 options.
1. Write the tar procedure in chunks, and in between chunks,
test for $die.

2. Find the pid of the tar command after it's launched in the thread,
and put it in a shared variable. Then have the main thread kill -9 it,
before you set $die = 1.


Just ideas,
zentara


--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Killing subservient threads jimzat Python 6 03-12-2009 03:34 PM
killing threads in perl prameela.vankineni@gmail.com Perl Misc 1 03-10-2006 12:08 PM
Killing threads in perl prameela.vankineni@gmail.com Perl Misc 1 03-09-2006 12:36 PM
Killing threads during development. Don Garrett Python 0 06-05-2005 08:41 PM
Start/Stop Threads Without Killing Them cppaddict Java 20 08-11-2004 07:20 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57