Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Killing threads (http://www.velocityreviews.com/forums/t896944-killing-threads.html)

prameela.vankineni@gmail.com 03-11-2006 03:54 AM

Killing threads
 
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);
}


zentara 03-11-2006 02:57 PM

Re: Killing threads
 
On 10 Mar 2006 19:54:29 -0800, prameela.vankineni@gmail.com 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

A. Sinan Unur 03-11-2006 03:07 PM

Re: Killing threads
 
zentara <zentara@highstream.net> wrote in
news:3jo5129kjmfmru14is2nnoc5ktla385n7u@4ax.com:

> On 10 Mar 2006 19:54:29 -0800, prameela.vankineni@gmail.com 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 <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html



All times are GMT. The time now is 09:12 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.