Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi.

I'm trying to write a web client that uses LWP and Tk. Unfortunately, calls to get() keep blocking and freezing-up the program for often-significant amounts of time while LWP sends the request and receives response. Is there some way to get around this besides using Thread.pm or threads.pm? I tried update(), but that didn't seem to work. What I really need is way to call get() (or request()) and get access to the response object without the windows freezing and becoming unresponse for a while. Here's some example code:

#!/usr/bin/perl -w use strict; use Tk; use Tk::Pane; use LWP; use vars qw($URL); use vars qw($DOC_VIEWER); use vars qw($DOC); my $lwp = new LWP::UserAgent; my $browser = new MainWindow; my $url_entry_frame = $browser->Frame->pack(-side => 'top', -padx => 3 +, -pady => 4); $url_entry_frame->Entry( -textvariable => \$URL, -width => 100 )->pack(-side => 'left', -padx => 1); $url_entry_frame->Button( -text => 'GO', -command => sub { sendRequest() } )->pack(-side => 'left', -padx => 1); $DOC_VIEWER = $browser->Scrolled('Pane', Name => 'doc_viewer', -scrollbars => 'se', -sticky => 'we', -gridded => 'y' )->pack(-expand => 1, -fill => 'both'); my $status = $browser->Label->pack(-side => 'bottom'); MainLoop; sub sendRequest { $DOC->destroy if ($DOC); $status->update; $status->configure(-text => 'Requesting...'); my $response = $lwp->get($URL); $status->configure(-text => 'Done.'); if ($response->is_success) { $DOC = $DOC_VIEWER->Label(-text => $response->content)->pack; } else { die $response->error_as_HTML; } }

Replies are listed 'Best First'.
Re: Tk, LWP, and blocking?
by saintmike (Vicar) on Mar 05, 2004 at 08:35 UTC
    You're looking at a fundamental problem with GUIs and synchronous I/O: While an I/O operation (a HTTP get in your case) is running, the GUI doesn't get refreshed, it looks "frozen".

    There's a couple of different ways to fix this. You could use multiple processes or threads (one GUI-refresher, one HTTP-getter) or you could use an asynchronous approach like POE.

    You've timed your question perfectly, because there's a new article out on how to do the latter: Using GUIs (Gtk in this case, but it works for Tk, too) and POE for I/O, all in one process, all smoothly:

    The April issue of the German Linux-Magazin features a GUI-driven stock ticker, fetching stock quotes via HTTP. The British "Linux-Magazine" (also available in the US at Barnes & Noble) will print the article in its May issue (update: now available here).

    Whichever language you prefer, the listing (at the bottom of the online article) should illustrate the general concept.

Re: Tk, LWP, and blocking?
by mawe (Hermit) on Mar 05, 2004 at 08:27 UTC
      Strange... Two of us posted pretty much the same question on the same day.
•Re: Tk, LWP, and blocking?
by merlyn (Sage) on Mar 05, 2004 at 15:56 UTC
Re: Tk, LWP, and blocking?
by zentara (Cardinal) on Mar 05, 2004 at 14:34 UTC
    use Tk::fileevent

    I'm not really a human, but I play one on earth. flash japh
Re: Tk, LWP, and blocking?
by smackdab (Pilgrim) on Mar 05, 2004 at 23:36 UTC
    One more idea, not sure you would want to modify LWP (or subclass it if possible...)

    You would probably need to make LWP use non-blocking sockets and also use select(). You would then have to do your own timeout loop and callback into your program to that you could call Tk::DoOneEven(ALL_EVENTS) to ensure the GUI keeps ticking

    Probably not too hard if you know socket communication, but likely not simple ;-)
Re: Tk, LWP, and blocking?
by Anonymous Monk on Mar 06, 2004 at 08:15 UTC

    Thanks everyone.

    I've decided to go with Mike and Randal's suggestion to learn and use POE. I've already started on it, and I must say, I'm quite impressed with it.