Killing threads

P

prameela.vankineni

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);
}
 
Z

zentara


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);
}
 
A

A. Sinan Unur

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top