threads problem

P

prameela

Hai All,


Iam using file transfer(IO ::Ftp) with progress bars
updation with threads.But it is showing cannot read memory error.Iam
giving my part of code here. please suggest me how to correct this.


sub on_btnAddToXfer_clicked
{
$statusbar->pop($context_id);
my $filename = get_selected_file;
my $model = $lstXferQ->get_model;
my $new_iter = $model->append;
$model->set($new_iter, 0 => $filename);
print "File is $filename\n";
my $data = get_story_data_from_widgets;
$fnames{$filename} = $data;
# Appending hash values to the transferring file
open(FD,">> $filename") or die "cannot open $filename: $!";
for my $key(keys %{$fnames{$filename}})
{
print FD $fnames{$filename}{$key};
#print $fnames{$filename}{$key};
}
close(FD);

my $name_idx = $cmbName->get_active;

if($name_idx != -1)
{
$statusbar->pop($context_id);
}
elsif($txtSlug->get_text ne '')
{
$statusbar->pop($context_id);
}
elsif($txtCategory->get_text ne '')
{
$statusbar->pop($context_id);
}
$thread = new threads(\&get_value,$filename) unless
(defined($thread));
}


sub split_file
{
my $filename = shift;
$totalfilesize = -s $filename;
print "File in split is : $filename\n $totalfilesize\n";
my ($name, $ext);
if ($filename =~ /(.*)\.(.*)$/)
{
$name = $1; $ext = $2;
}

# no of segments hardcoded to 5

my $file_size = -s $filename;
my ($segment_size, $rem);
$segment_size = $file_size / NSEGMENTS;
$rem = $file_size % NSEGMENTS;

open(FD,"< $filename") or die "cannot open $filename: $!";
binmode(FD);

my $buffer;
my @retval_filenames;
#Splitting the file into segments
for (my $i = 1; $i <=NSEGMENTS; $i++)
{
my $outfile = "$name-$i.$ext";
open (OUTFD, "> $outfile") or die "Failed to open $outfile:
$!";
binmode(OUTFD);
$segment_size += $rem if ($i == NSEGMENTS);
read(FD,$buffer, $segment_size);
print OUTFD $buffer;
close(OUTFD);
push @retval_filenames, $outfile;
}
close (FD);
return \@retval_filenames;
}


sub transfer_file
{
my $arrayref = shift;
my $filename = @$arrayref[0];
my $count = @$arrayref[1];
print "FILE IS : $filename\n";
my $file_size = -s $filename;
my $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/squick/$org_file",TYPE=>'i');
my $n;
my $buffer;
while($n = read(FD,$buffer,4*1024*1024))
{
$ftp->send($buffer);
$completed += $n;
print "C is $completed\n";
$finished += $n;
print "F is : $finished\n";
# % xferred
my $percent = $completed / $file_size;
$progressbar[$count]->set_fraction($percent);
#~ my $totalpercent = $finished / $totalfilesize;
#~ print "Total is $totalpercent\n";
#~ if($totalpercent <= 1)
#~ {
#~ $progressbar6->set_fraction($totalpercent);
#~ }
undef($buffer);
if($ftp_die == 1)
{
$ftp->close;
$ftp->delete($filename);
unlink($filename);
&clear_progressbars;
print "File deleted\n";
return;
}
}
if($finished == $totalfilesize )#or $finished != $totalfilesize)
{
sleep(1);
$finished = 0;
print "cleared\n";
&clear_progressbars;
}
close(FD);
# Deleting temporary files from the Directory
unlink($filename);
}


sub get_value
{
print "Monitoring...\n";
while(1)
{
next unless (defined($list));
my $iter = $list->get_iter_first;
my $filename = $list->get($iter, 0) if defined($iter);
if($is_filexfer ==1)
{
my $totalpercent = $finished / $totalfilesize;
print "Total is $totalpercent\n";
if($totalpercent > 1)
{
print "total exceeds one\n";
$totalpercent = 1;
}
if($totalpercent <= 1)
{
$progressbar6->set_fraction($totalpercent);
}
if($totalpercent >= 1)
{
$is_filexfer = 0 ;
sleep(1);
}
}
if($is_filexfer == 0)
{
next unless(defined($filename));
print"File name 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);
$is_filexfer = 1 ;
$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;

}
 
Z

zentara

Hai All,
Iam using file transfer(IO ::Ftp) with progress bars
updation with threads.But it is showing cannot read memory error.Iam
giving my part of code here. please suggest me how to correct this.

You really are poor at asking a question.

First you don't even mention
that you are trying to use threads under Gtk2.
Gtk2 handles threads differently than normal Perl.

Second, you say you get an error, but you don't show(or discuss) where
in your code snippet your error occurred.

Without a working code example, which demonstrates your problem,
no one is going to figure out why your pseudo-code dosn't work.

At first glance, it looks like you are using threads to launch other
threads, where get_value is a thread code block, which launches yet
another code block transfer_file_cb.

This is a mess of spaghetti.

In using Gtk2 threads, you should start your script with

use Gtk2 qw/-init -threads-init/;

#this line causes the warning, but dosn't die
die "Glib::Object thread safetly failed"
unless Glib::Object->set_threadsafe (TRUE);

then use the new enter and leave methods, to tell
the main gui eventloop that you are switching to a thread block

Gtk2::Gdk::Threads->enter;
#$prog->pulse;
$prog->set_fraction ($num/20);
$prog->set_text ($num/20 * 100 .'%');

Gtk2::Gdk::Threads->leave;

Other than those 2 bits of advise, I can't tell you what
your problem may be.

Look at the thread demo that comes with the Perl/Gtk2 module.

Start with simple threaded prototypes first, then as you
feel they are solid and controllable, increase complexity.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top