Killing threads in perl

Discussion in 'Perl Misc' started by joergwenzel@gmx.de, Mar 7, 2007.

  1. Guest

    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", -$$);

    }

    }
     
    , Mar 7, 2007
    #1
    1. Advertising

  2. gf Guest

    On Mar 7, 7:44 am, 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
     
    gf, Mar 7, 2007
    #2
    1. Advertising

  3. 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
     
    Eric Schwartz, Mar 7, 2007
    #3
  4. zentara Guest

    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
     
    zentara, Mar 8, 2007
    #4
  5. Guest


    > 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
     
    , Mar 9, 2007
    #5
  6. zentara Guest

    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
     
    zentara, Mar 9, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. cppaddict
    Replies:
    20
    Views:
    821
    Chris Uppal
    Aug 11, 2004
  2. Don Garrett

    Killing threads during development.

    Don Garrett, Jun 5, 2005, in forum: Python
    Replies:
    0
    Views:
    307
    Don Garrett
    Jun 5, 2005
  3. jimzat

    Killing subservient threads

    jimzat, Feb 20, 2009, in forum: Python
    Replies:
    6
    Views:
    326
  4. Killing threads in perl

    , Mar 9, 2006, in forum: Perl Misc
    Replies:
    1
    Views:
    99
    zentara
    Mar 9, 2006
  5. killing threads in perl

    , Mar 9, 2006, in forum: Perl Misc
    Replies:
    1
    Views:
    100
    zentara
    Mar 10, 2006
Loading...

Share This Page