For Programmers: Free Programming Magazines  


Home > Archive > PerlTk > March 2007 > sleep hangs program









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author sleep hangs program
jcf

2007-03-18, 10:05 pm


I have a canvas that I am plotting points to. I want this plotting to
be animated so the plotted points will be ploted gradually instead of
having the whole thing displayed in one quick instance. So I put a
sleep 2 in the plotting loop and the there is not a 2 second delay
between each point being plotted. Instead the program hangs and I have
to do a CTL-C to kill the program.

Why is this happening and not behaving like I expect it to?

This in on a Mac OS X 10.4.8.

Here is a code snippet.

NOTE: $Canv is a reference to the canvas that I am drawing to.

for($ii = 0, $ij=0; $ii<400; $ii++, $ij++)
{
plot($Canv, $ii, $ij, 'blue');
sleep 2 ;
}

sub plot
{
local($da, $x, $y, $color) = @_;
$da->createText($x, $y, -fill => $color, -text => '.');
}

Marc Dashevsky

2007-03-18, 10:05 pm

In article <1174263172.602385.168330@n59g2000hsh.googlegroups.com>, jcf2001@hotmail.com says...
>
> I have a canvas that I am plotting points to. I want this plotting to
> be animated so the plotted points will be ploted gradually instead of
> having the whole thing displayed in one quick instance. So I put a
> sleep 2 in the plotting loop and the there is not a 2 second delay
> between each point being plotted. Instead the program hangs and I have
> to do a CTL-C to kill the program.


Read about the after() method: perldoc Tk::after


--
Go to http://MarcDashevsky.com to send me e-mail.
jcf

2007-03-18, 10:05 pm

On Mar 18, 8:20 pm, Marc Dashevsky <use...@MarcDashevsky.com> wrote:
> In article <1174263172.602385.168...@n59g2000hsh.googlegroups.com>, jcf2...@hotmail.com says...
>
>
>
>
> Read about the after() method: perldoc Tk::after
>
> --
> Go tohttp://MarcDashevsky.comto send me e-mail.



I just finished trying that and it could not get it to work. I got the
same results that I did when I tried sleep.

Can you show me an example using my code sinppet?



Marc Dashevsky

2007-03-18, 10:05 pm

In article <1174265484.004737.54680@b75g2000hsg.googlegroups.com>, jcf2001@hotmail.com says...
> On Mar 18, 8:20 pm, Marc Dashevsky <use...@MarcDashevsky.com> wrote:
>
>
> I just finished trying that and it could not get it to work. I got the
> same results that I did when I tried sleep.
>
> Can you show me an example using my code sinppet?


You show me yours first! Oh, all right:

use strict;
use warnings;
use Tk;

my $Coord = 0;
my $mw = tkinit;
my $cv = $mw->Canvas->pack;
$mw->after(500, [\&plot, $cv]);
MainLoop;

sub plot {
my($canvas) = @_;
$canvas->createText($Coord, $Coord, -text => '+');
$Coord += 2;
$canvas->after(100, [\&plot, $canvas]);
}



--
Go to http://MarcDashevsky.com to send me e-mail.
jcf

2007-03-19, 7:07 pm

On Mar 18, 9:13 pm, Marc Dashevsky <use...@MarcDashevsky.com> wrote:
> In article <1174265484.004737.54...@b75g2000hsg.googlegroups.com>, jcf2...@hotmail.com says...
>
>
>
>
>
>
>
>
> You show me yours first! Oh, all right:
>
> use strict;
> use warnings;
> use Tk;
>
> my $Coord = 0;
> my $mw = tkinit;
> my $cv = $mw->Canvas->pack;
> $mw->after(500, [\&plot, $cv]);
> MainLoop;
>
> sub plot {
> my($canvas) = @_;
> $canvas->createText($Coord, $Coord, -text => '+');
> $Coord += 2;
> $canvas->after(100, [\&plot, $canvas]);
> }
>
> --
> Go tohttp://MarcDashevsky.comto send me e-mail.


Your sample works but I cannot get it to work within my program. Your
example
has a different structure than mine.

Here is what I am trying to do in psudo code.

sub run_cluster
while not stop
for loop through list of things
do some calculations on x and y
end for
for loop through list of things
do some more calculations on x and y

sleep for 1 second
plot x, y onto the canvas
end for
end while
end run_cluster

When I try to integrate your example into this structure it hangs.
How can I use after in this situation?


Ch Lamprecht

2007-03-19, 7:07 pm

jcf wrote:
> On Mar 18, 9:13 pm, Marc Dashevsky <use...@MarcDashevsky.com> wrote:
>
<snip>[color=darkred]
>
> Your sample works but I cannot get it to work within my program. Your
> example
> has a different structure than mine.
>
> Here is what I am trying to do in psudo code.


It will be easier to help you if you post real code.
>
> sub run_cluster
> while not stop
> for loop through list of things
> do some calculations on x and y
> end for
> for loop through list of things
> do some more calculations on x and y

$main_window->update;
>
> sleep for 1 second
> plot x, y onto the canvas
> end for
> end while
> end run_cluster
>
> When I try to integrate your example into this structure it hangs.
> How can I use after in this situation?
>
>

Christoph

--
use Tk;use Tk::GraphItems;$c=tkinit->Canvas->pack;push@i,Tk::GraphItems->
TextBox(text=>$_,canvas=>$c,x=>$x+=70,y=>100)for(Just=>another=>Perl=>Hacker);
Tk::GraphItems->Connector(source=>$i[$_],target=>$i[$_+1])for(0..2);
$c->repeat(30,sub{$_->move(0,4*cos($d+=3.16))for(@i)});MainLoop
Marc Dashevsky

2007-03-19, 7:07 pm

In article <1174312018.234159.145170@p15g2000hsd.googlegroups.com>, jcf2001@hotmail.com says...
>
> Your sample works but I cannot get it to work within my program. Your
> example has a different structure than mine.


I was hoping to show you another way to think about it.
A GUI program is sometimes structured differently than
you might imagine.

--
Go to http://MarcDashevsky.com to send me e-mail.
jcf

2007-03-19, 10:04 pm

On Mar 19, 10:48 am, Marc Dashevsky <use...@MarcDashevsky.com> wrote:
> In article <1174312018.234159.145...@p15g2000hsd.googlegroups.com>, jcf2...@hotmail.com says...
>
>
>
>
> I was hoping to show you another way to think about it.
> A GUI program is sometimes structured differently than
> you might imagine.
>
> --
> Go tohttp://MarcDashevsky.comto send me e-mail.



I got it working like I want but I didn't need to use the after
method.
Using $main_window->update seemed to solve the hang problem.

It seem that the combination of my number crunching and the slowness
of perl is enough to give me what I want for now.

Now I have to figure out how to clear the canvas.

Thanks.

zentara

2007-03-20, 7:04 pm

On 19 Mar 2007 18:41:50 -0700, "jcf" <jcf2001@hotmail.com> wrote:

>Now I have to figure out how to clear the canvas.
>Thanks.


Here is a simple example:
Basically it's
canvas->delete('all');


#!/usr/bin/perl
use warnings;
use strict;
use Tk;

# the -stipple=>'transparent' option will still
# allow the bindings to work, but you can see the overlap
# See Chapter 17 of Mastering Perl/Tk

my $mw = MainWindow->new();

# first create a canvas widget
my $canvas = $mw->Canvas(width => 300, height => 200)->pack();

my $one = $canvas->createOval(55, 20, 200, 190,
-fill => 'blue',
-outline=>'blue',
-tags => ['blue'],
-stipple => 'transparent',
);

my $two = $canvas->createOval(105, 20, 250, 190,
-fill => 'red',
-outline=>'red',
-tags => ['red'],
-stipple => 'transparent',
);


my $ebutton = $mw->Button(-text => 'Exit',
-command => 'Tk::exit')->pack();

my $cbutton = $mw->Button(-text => 'Clear',
-command => sub{$canvas->delete('all')})->pack();


$canvas->Tk::bind("<Motion>", [ \&print_xy, Ev('x'), Ev('y') ]);

MainLoop();

sub print_xy {
my ($canv, $x, $y) = @_;
# print "(x,y) = ", $canv->canvasx($x), ", ", $canv->canvasy($y), "\n";

#trick to find overlapping objects, just use x1 = x and y1 = y
#to get a rectangular region of 1 point
my (@current) = $canvas->find('overlapping', $x, $y, $x, $y);

foreach my $id(@current){
print $canvas->gettags($id),' ';
}

print "\n";

}
__END__



--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
jcf

2007-03-20, 7:04 pm

On Mar 20, 10:20 am, zentara <zent...@highstream.net> wrote:
> On 19 Mar 2007 18:41:50 -0700, "jcf" <jcf2...@hotmail.com> wrote:
>
>
> Here is a simple example:
> Basically it's
> canvas->delete('all');
>
> #!/usr/bin/perl
> use warnings;
> use strict;
> use Tk;
> # allow the bindings to work, but you can see the overlap
> # See Chapter 17 of Mastering Perl/Tk
>
> my $mw = MainWindow->new();
>
> # first create a canvas widget
> my $canvas = $mw->Canvas(width => 300, height => 200)->pack();
>
> my $one = $canvas->createOval(55, 20, 200, 190,
> -fill => 'blue',
> -outline=>'blue',
> -tags => ['blue'],
> -stipple => 'transparent',
> );
>
> my $two = $canvas->createOval(105, 20, 250, 190,
> -fill => 'red',
> -outline=>'red',
> -tags => ['red'],
> -stipple => 'transparent',
> );
>
> my $ebutton = $mw->Button(-text => 'Exit',
> -command => 'Tk::exit')->pack();
>
> my $cbutton = $mw->Button(-text => 'Clear',
> -command => sub{$canvas->delete('all')})->pack();
>
> $canvas->Tk::bind("<Motion>", [ \&print_xy, Ev('x'), Ev('y') ]);
>
> MainLoop();
>
> sub print_xy {
> my ($canv, $x, $y) = @_;
> # print "(x,y) = ", $canv->canvasx($x), ", ", $canv->canvasy($y), "\n";
>
> #trick to find overlapping objects, just use x1 = x and y1 = y
> #to get a rectangular region of 1 point
> my (@current) = $canvas->find('overlapping', $x, $y, $x, $y);
>
> foreach my $id(@current){
> print $canvas->gettags($id),' ';
>
> }
>
> print "\n";
>
> }
>
> __END__
>
> --
> I'm not really a human, but I play one on earth.http://zentara.net/japh.html



It worked. Thanks.

Slaven Rezic

2007-03-22, 7:01 pm

"jcf" <jcf2001@hotmail.com> writes:

> On Mar 19, 10:48 am, Marc Dashevsky <use...@MarcDashevsky.com> wrote:
>
>
> I got it working like I want but I didn't need to use the after
> method.
> Using $main_window->update seemed to solve the hang problem.
>


An better solution would be a combination of waitVariable and after.
Try the following script, maybe one day it will be part of the
official Perl/Tk distribution:

=head2 tk_sleep

=for category Tk

$top->tk_sleep($s);

Sleep $s seconds (fractions are allowed). Use this method in Tk
programs rather than the blocking sleep function. The difference to
$top->after($s/1000) is that refresh events are still handled in the
sleeping time.

=cut

sub Tk::Widget::tk_sleep {
my($top, $s) = @_;
my $sleep_dummy = 0;
$top->after($s*1000,
sub { $sleep_dummy++ });
$top->waitVariable(\$sleep_dummy)
unless $sleep_dummy;
}


--
Slaven Rezic - slaven <at> rezic <dot> de

Tk-AppMaster: a perl/Tk module launcher designed for handhelds
http://tk-appmaster.sf.net
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com