Killing threads

Discussion in 'Perl Misc' started by prameela.vankineni@gmail.com, Mar 11, 2006.

  1. Guest

    Hi,

    Iam trying to transfer files(Io:Ftp)using threads in perl can any
    one suggest me how to stop threads if i pressed cancelTransfer
    button.Here iam giving part of my code.

    sub get_value

    {

    print "Monitoring...\n";

    while(1)

    {

    next unless (defined($list));

    #sleep(2);

    my $iter = $list->get_iter_first;

    my $filename = $list->get($iter, 0) if defined($iter);

    next unless(defined($filename));

    print "File is $filename\n";

    print "Splitting file...\n";

    my $filenames_ref = split_file($filename);

    my @filenames = @$filenames_ref;

    $count = 0;

    $finished = 0;

    foreach (@filenames)

    {

    print "Transferring $_ ...\n";

    transfer_file_cb($_,$count);

    $count++;

    }

    sleep(2);

    $list->remove($iter) if defined($iter);

    }

    }

    sub transfer_file_cb

    {

    my $thread = new threads(\&transfer_file,\@_);

    push (@threads, $thread);

    }

    # wait for the children to finish...

    foreach (@threads)

    {

    $_->join;
    }


    sub transfer_file
    {
    my $arrayref = shift;
    my $filename = @$arrayref[0];
    my $count = @$arrayref[1];
    print "FILE IS : $filename\n";
    my $file_size = -s $filename;
    $org_file;

    #Taking only filename from the path
    if($filename =~ /([A-Za-z0-9_-]*\.mpg)$/)
    {
    $org_file = $1;
    }
    open(FD,"$filename") or die "cannot open file: $!";
    binmode(FD);
    my $completed;
    my
    $ftp=IO::Ftp->new(">","//user:usr1234\@callista.ueltv.org/quick/$org_file",TYPE=>'i');
    my $n;
    while($n = read(FD,$buffer,4*1024*1024))
    {
    $ftp->send($buffer);
    $completed += $n;
    $finished += $n;

    # % xferred
    my $percent = $completed / $file_size;
    $progressbar[$count]->set_fraction($percent);
    my $totalpercent = $finished / $totalfilesize;
    print "Total is $totalpercent\n";
    print "F is $finished\n";
    $progressbar6->set_fraction($totalpercent);
    undef($buffer);
    }
    if($finished == $totalfilesize)
    {
    $finished = 0;
    $progressbar[0]->set_fraction('');
    $progressbar[1]->set_fraction('');
    $progressbar[2]->set_fraction('');
    $progressbar[3]->set_fraction('');
    $progressbar[4]->set_fraction('');
    $progressbar6->set_fraction('');
    }
    close(FD);
    # Deleting temporary files from the Directory

    unlink($filename);
    }
     
    , Mar 11, 2006
    #1
    1. Advertising

  2. zentara Guest

    On 10 Mar 2006 19:54:29 -0800, wrote:

    >Hi,


    Yeah, try playing with simple examples, of setting up and cancelling
    threads, before trying to do it with FTP.

    #!/usr/bin/perl
    use warnings;
    use strict;
    use threads;
    use threads::shared;

    $| = 1;

    my %threads;

    foreach (1..10){
    share ($threads{$_}{'die'});
    $threads{$_}{'die'} = 0;
    }

    foreach (1..10) {
    $threads{$_}{'thread'} = threads->new('StartTest');
    }

    my @threads = (1..10);
    foreach(@threads){
    $threads{$_}{'die'} = 1;
    $threads{$_}{'thread'}->join;
    }

    print "\n", "All threads done\n";

    sub StartTest {
    my $self = threads->self;

    print "Thread ", $self->tid, " started\n";

    while(1){
    if( $threads{$_}{'die'} == 1){goto FINISH}
    else{ select(undef,undef,undef, 10) };
    }
    FINISH:
    print "Thread ", $self->tid, " ending\n";
    }
    __END__

    > Iam trying to transfer files(Io:Ftp)using threads in perl can any
    >one suggest me how to stop threads if i pressed cancelTransfer
    >button.Here iam giving part of my code.
    >
    >sub get_value
    >
    >{
    >
    > print "Monitoring...\n";
    >
    > while(1)
    >
    > {
    >
    > next unless (defined($list));
    >
    > #sleep(2);
    >
    > my $iter = $list->get_iter_first;
    >
    > my $filename = $list->get($iter, 0) if defined($iter);
    >
    > next unless(defined($filename));
    >
    > print "File is $filename\n";
    >
    > print "Splitting file...\n";
    >
    > my $filenames_ref = split_file($filename);
    >
    > my @filenames = @$filenames_ref;
    >
    > $count = 0;
    >
    > $finished = 0;
    >
    > foreach (@filenames)
    >
    > {
    >
    > print "Transferring $_ ...\n";
    >
    > transfer_file_cb($_,$count);
    >
    > $count++;
    >
    > }
    >
    > sleep(2);
    >
    > $list->remove($iter) if defined($iter);
    >
    > }
    >
    >}
    >
    >sub transfer_file_cb
    >
    >{
    >
    > my $thread = new threads(\&transfer_file,\@_);
    >
    > push (@threads, $thread);
    >
    >}
    >
    > # wait for the children to finish...
    >
    >foreach (@threads)
    >
    >{
    >
    > $_->join;
    >}
    >
    >
    >sub transfer_file
    >{
    > my $arrayref = shift;
    > my $filename = @$arrayref[0];
    > my $count = @$arrayref[1];
    > print "FILE IS : $filename\n";
    > my $file_size = -s $filename;
    > $org_file;
    >
    > #Taking only filename from the path
    > if($filename =~ /([A-Za-z0-9_-]*\.mpg)$/)
    > {
    > $org_file = $1;
    > }
    > open(FD,"$filename") or die "cannot open file: $!";
    > binmode(FD);
    > my $completed;
    > my
    >$ftp=IO::Ftp->new(">","//user:usr1234\@callista.ueltv.org/quick/$org_file",TYPE=>'i');
    > my $n;
    > while($n = read(FD,$buffer,4*1024*1024))
    > {
    > $ftp->send($buffer);
    > $completed += $n;
    > $finished += $n;

    In main, setup a shared variable
    share $ftp_die;
    $ftp_die = 0;

    Then in your cancel button callback, set $ftp_die =1;
    then join the thread.


    Right in this callback for ftp bytes transfer, put a test for a
    shared variable, like this pseudo_code:

    if( $ftp_die ==1 ){ $ftp->cancel; #or whatever IO::Ftp uses
    clean_up_temp_files();
    return;
    # will end the thread
    }


    > # % xferred
    > my $percent = $completed / $file_size;
    > $progressbar[$count]->set_fraction($percent);
    > my $totalpercent = $finished / $totalfilesize;
    > print "Total is $totalpercent\n";
    > print "F is $finished\n";
    > $progressbar6->set_fraction($totalpercent);
    > undef($buffer);
    > }
    > if($finished == $totalfilesize)
    > {
    > $finished = 0;
    > $progressbar[0]->set_fraction('');
    > $progressbar[1]->set_fraction('');
    > $progressbar[2]->set_fraction('');
    > $progressbar[3]->set_fraction('');
    > $progressbar[4]->set_fraction('');
    > $progressbar6->set_fraction('');
    > }
    > close(FD);
    > # Deleting temporary files from the Directory
    >
    > unlink($filename);
    >}


    --
    I'm not really a human, but I play one on earth.
    http://zentara.net/japh.html
     
    zentara, Mar 11, 2006
    #2
    1. Advertising

  3. zentara <> wrote in
    news::

    > On 10 Mar 2006 19:54:29 -0800, wrote:
    >
    >>Hi,

    >
    > Yeah, try playing with simple examples, of setting up and cancelling
    > threads, before trying to do it with FTP.


    And follow-up under the original thread in the newsgroup. Don't keep
    starting new threads.

    I had not read any of your messages because I kept seeing these new
    messages from you with the same subject line as before, so I assumed you
    were just repeating your question every day.

    Please read and follow the posting guidelines for this group.

    Sinan

    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Mar 11, 2006
    #3
    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:
    823
    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

    , Apr 5, 2009, in forum: Python
    Replies:
    13
    Views:
    537
  5. Chris Angelico
    Replies:
    2
    Views:
    278
    Antoon Pardon
    Sep 21, 2011
Loading...

Share This Page