Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Parallel download Tk

by bliako (Monsignor)
on Dec 31, 2018 at 10:04 UTC ( [id://1227835]=note: print w/replies, xml ) Need Help??


in reply to Parallel download Tk

In a similar question: LWP::UserAgent timeout, I have suggested HTTP::Async which downloads a list of urls and reports back when done. If you want a list of urls to be fetched in parallel, there is LWP::Parallel::UserAgent. The latter will block until all urls are fethced. But they are fetched in parallel, so bandwidth and server allowing, it will probably be faster than sequential download. The former will not block, you check on progress by asking it.

However, the bottomline on answers on said question, and in this one too, is that you need to familiarise yourself with Tk::IO because:

I'd just add that, in either case, given that you are dealing with a GUI, it's usually a best practice to use a separate thread for the UI events processing and another for the processing (markong):

So, elevate yourself this festive season with multi-threading...

Replies are listed 'Best First'.
Re^2: Parallel download Tk
by Takamoto (Monk) on Dec 31, 2018 at 11:00 UTC

    I guess, I underestimated the complexity of the subject. I'll have a closer look at Tk::IO. Unfortunately the documentation is quite short (almost nonexistent).

      Here's an example of Tk::IO

      You'll have to write a separate perl program to fetch URLs. In this example I faked that part with a simple shell script just as a proof of concept.

      #!/usr/bin/perl # https://perlmonks.org/?node_id=1227829 use strict; use warnings; use Tk; use Tk::IO; use Tk::ROText; my (@data1, @data2, @data3, @data4); my $complete1 = my $complete2 = my $complete3 = my $complete4 = 0; my $status = 'Ready to Start'; my $mw = MainWindow->new; $mw->Button(-text => 'Load', -command => \&startload, )->pack; $mw->Label(-textvariable => \$status, )->pack; $mw->Button(-text => 'Exit', -command => sub{$mw->destroy}, )->pack(-side => 'bottom'); $_ = $mw->ROText( -width => 40, )->pack(-side => 'left') for my ($t1, $t2, $t3, $t4); MainLoop; sub startload { $status = 'Started'; child( \@data1, \$complete1, 'one', 'sleep 1; echo data one', $t1 ); child( \@data2, \$complete2, 'two', 'sleep 3; echo data two', $t2 ); child( \@data3, \$complete3, 'three', 'sleep 4; echo data three', $t +3 ); child( \@data4, \$complete4, 'four', 'sleep 2; echo data four', $t4 +); } sub common { $complete1 && $complete2 && $complete3 && $complete4 or return; $status = 'All Completed'; # do final processing here ##################### } sub child { my ($refdata, $refcomplete, $message, $command, $rotext) = @_; @$refdata = (); $$refcomplete = 0; $rotext->delete('1.0' => 'end'); Tk::IO->new( -linecommand => sub {push @$refdata, shift}, -childcommand => sub { $$refcomplete = 1; $status = "$message completed"; $rotext->insert(end => join '', @$refdata); common(); }, )->exec($command); }

      There are a couple other examples of Tk::IO on this site, you can search for them if you want.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (9)
As of 2024-04-23 21:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found