Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^4: Perl Tk freezes when command button is clicked !

by mykl (Monk)
on Feb 17, 2010 at 15:03 UTC ( [id://823723]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Perl Tk freezes when command button is clicked !
in thread Perl Tk freezes when command button is clicked !

As an alternative to using threads, it is also possible to unblock the event loop by manually calling $mw->update() at frequent intervals to alllow the GUI to update, respond etc. This may or may not be easier/better for you, depending on what you are trying to do. I think it doesn't matter what Tk object you call the update() method on, although I usually use the main window.

--

"Any sufficiently analyzed magic is indistinguishable from science" - Agatha Heterodyne

Replies are listed 'Best First'.
Re^5: Perl Tk freezes when command button is clicked !
by zentara (Archbishop) on Feb 17, 2010 at 19:05 UTC
    The problem with using $mw->update or DoOneLoop() is that they still will block on slow network calls that are sluggish. When you use $lwp->get( $url), there is no place to insert the update, unless you use the more extended manual callback available in lwp, as shown in
    #!/usr/bin/perl -w use strict; use LWP::UserAgent; # don't buffer the prints to make the status update $| = 1; my $ua = LWP::UserAgent->new(); my $received_size = 0; my $url = 'http://www.cpan.org/authors/id/J/JG/JGOFF/parrot-0_0_7.tgz' +; print "Fetching $url\n"; my $request_time = time; my $last_update = 0; my $response = $ua->get($url, ':content_cb' => \&callback, ':read_size_hint' => 8192, ); print "\n"; sub callback { my ($data, $response, $protocol) = @_; my $total_size = $response->header('Content-Length') || 0; $received_size += length $data; my $time_now = time; # this to make the status only update once per second. return unless $time_now > $last_update or $received_size == $total_s +ize; $last_update = $time_now; print "\rReceived $received_size bytes"; printf " (%i%%)", (100/$total_size)*$received_size if $total_size; printf " %6.1f/bps", $received_size/(($time_now-$request_time)||1) if $received_size; }
    If you don't put your update calls into that callback, it will still block Tk until the get returns. And if the get() hangs, your Tk program could be hung forever, unless you set timers on it.

    But it really is easier just to use a thread. :-)


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://823723]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2024-04-26 02:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found