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

I wanted to create watch folder for download and upload files from/to server for every 3 seconds for 21 accounts

If i use the below code using TK, the window will automaticaly get quit, when i click the start button of 4th button. Inside while loop i am going to call all the relevent scripts(copy/paste). The same i wanted do for 21 accounts.

use threads; use threads::shared; my $dummy1downloadgo : shared = 0; my $dummy1downloadvalue : shared = "IDLE"; my $dummy1uploadgo : shared = 0; my $dummy1uploadvalue : shared = "IDLE"; ### dummy2 SHARED#### my $dummy2downloadgo : shared = 0; my $dummy2downloadvalue : shared = "IDLE"; my $dummy2uploadgo : shared = 0; my $dummy2uploadvalue : shared = "IDLE"; my $dummy3downloadgo : shared = 0; my $dummy3downloadvalue : shared = "IDLE"; my $dummy3uploadgo : shared = 0; my $dummy3uploadvalue : shared = "IDLE"; ##### dummy1 THREAD ##### my $dummy1downloadThread = threads->new( \&dummy1downloadthread ); my $dummy1uploadThread = threads->new( \&dummy1uploadthread ); ##### dummy2 THREAD ##### my $dummy2downloadThread = threads->new( \&dummy2downloadthread ); my $dummy2uploadThread = threads->new( \&dummy2uploadthread ); ##### dummy3 THREAD ##### my $dummy3downloadThread = threads->new( \&dummy3downloadthread ); my $dummy3uploadThread = threads->new( \&dummy3uploadthread ); ##### dummy1 DOWNLOAD PROCESS ##### sub dummy1downloadthread { while (1) { if ($dummy1downloadgo == 0) { sleep 2; $dummy1downloadvalue = "IDLE" ; } else { #$dummy1downloadvalue++; $dummy1downloadvalue = "dummy1 Download Process 1" ; #print "$myValue\n"; sleep 2; $dummy1downloadvalue = "dummy1 Download Process 2" ; #print "$myValue\n"; sleep 2; } } } ##### dummy1 UPLOAD PROCESS ##### sub dummy1uploadthread { while (1) { if ($dummy1uploadgo == 0) { sleep 2; $dummy1uploadvalue = "IDLE" ; } else { #$dummy1uploadvalue++; $dummy1uploadvalue = "dummy1 Upload Process 1" ; #print "$myValue\n"; sleep 2; $dummy1uploadvalue = "dummy1 Upload Process 2" ; #print "$myValue\n"; sleep 2; } } } ##### dummy2 DOWNLOAD PROCESS ##### sub dummy2downloadthread { while (1) { if ($dummy2downloadgo == 0) { sleep 2; $dummy2downloadvalue = "IDLE" ; } else { #$dummy2downloadvalue++; $dummy2downloadvalue = "dummy2 Download Process 1" ; #print "$myValue\n"; sleep 2; $dummy2downloadvalue = "dummy2 Download Process 2" ; #print "$myValue\n"; sleep 2; } } } ##### dummy2 DOWNLOAD PROCESS ##### sub dummy2uploadthread { while (1) { if ($dummy2uploadgo == 0) { sleep 2; $dummy2uploadvalue = "IDLE" ; } else { #$dummy2uploadvalue++; $dummy2uploadvalue = "dummy2 Upload Process 1" ; #print "$myValue\n"; sleep 2; $dummy2uploadvalue = "dummy2 Upload Process 2" ; #print "$myValue\n"; sleep 2; } } } ##### dummy3 DOWNLOAD PROCESS ##### sub dummy3downloadthread { while (1) { if ($dummy3downloadgo == 0) { sleep 2; $dummy3downloadvalue = "IDLE" ; } else { #$dummy3downloadvalue++; $dummy3downloadvalue = "dummy3 Download Process 1" ; #print "$myValue\n"; sleep 2; $dummy3downloadvalue = "dummy3 Download Process 2" ; #print "$myValue\n"; sleep 2; } } } ##### dummy3 DOWNLOAD PROCESS ##### sub dummy3uploadthread { while (1) { if ($dummy3uploadgo == 0) { sleep 2; $dummy3uploadvalue = "IDLE" ; } else { #$dummy3uploadvalue++; $dummy3uploadvalue = "dummy3 Upload Process 1" ; #print "$myValue\n"; sleep 2; $dummy3uploadvalue = "dummy3 Upload Process 2" ; #print "$myValue\n"; sleep 2; } } } use Tk; use Tk::dialog; require Tk::NoteBook; require Tk::LabFrame; use Tk::LabFrame; my $mw = MainWindow->new(-background => 'gray50'); #my $lframe = $mw->Frame( -background => 'gray50',-borderwidth=>10 )-> +pack(-side =>'left' ,-fill=>'y'); #my $rframe = $mw->Frame( -background => 'gray50',-borderwidth=>10 )-> +pack(-side =>'right',-fill =>'both' ); my $mbar = $mw -> Menu(); $mw -> configure(-menu => $mbar); my %actives = (); #hash to hold reusable numbered widgets used for dow +nloads my @ready = (); #array to hold markers indicating activity is needed my $file = $mbar -> cascade(-label=>"File", -underline=>0, -tearoff => + 0); my $help = $mbar -> cascade(-label =>"Help", -underline=>0, -tearoff = +> 0); $file -> command(-label =>"Exit", -underline => 1, -command => sub { exit } ); $help -> command(-label =>"About", -underline => 0, -command => [\&menuClicked, "About"]); $mw->geometry("200x200"); $mw->title( 'dummy check' ); $nb = $mw->NoteBook(-borderwidth => 2 )->pack(-expand => 1, -fill => ' +both',); $p1 = $nb->add('page1', -label => 'dummy1'); my $dummy1downloadframe = $p1->LabFrame(-label => "dummy1 Download", -labelside => "acrosstop")->pack(); $dummy1downloadframe->Button( -text => 'Start', -command => \&dummy1do +wnloadstart )->pack( -side =>'left' ); $dummy1downloadframe->Button( -text => 'Stop', -command => \&dummy1dow +nloadstop )->pack( -side =>'left' ); $dummy1downloadstatus = $dummy1downloadframe->Label(-text => $dummy1do +wnloadvalue, -width => 40, -font=>"Verdana 10 bold")->pack( -side => +'left' ); my $dummy1uploadframe = $p1->LabFrame(-label => "dummy1 upload", -labelside => "acrosstop")->pack(); $dummy1uploadframe->Button( -text => 'Start', -command => \&dummy1uplo +adstart )->pack( -side =>'left' ); $dummy1uploadframe->Button( -text => 'Stop', -command => \&dummy1uploa +dstop )->pack( -side =>'left' ); $dummy1uploadstatus = $dummy1uploadframe->Label(-text => $dummy1upload +value, -width => 40, -font=>"Verdana 10 bold")->pack( -side => 'left' + ); $p2 = $nb->add('page2', -label => 'dummy2'); my $dummy2downloadframe = $p2->LabFrame(-label => "dummy2 Download", -labelside => "acrosstop")->pack(); $dummy2downloadframe->Button( -text => 'Start', -command => \&dummy2do +wnloadstart )->pack( -side =>'left' ); $dummy2downloadframe->Button( -text => 'Stop', -command => \&dummy2dow +nloadstop )->pack( -side =>'left' ); $dummy2downloadstatus = $dummy2downloadframe->Label(-text => $dummy2do +wnloadvalue, -width => 40, -font=>"Verdana 10 bold")->pack( -side => +'left' ); my $dummy2uploadframe = $p2->LabFrame(-label => "dummy2 upload", -labelside => "acrosstop")->pack(); $dummy2uploadframe->Button( -text => 'Start', -command => \&dummy2uplo +adstart )->pack( -side =>'left' ); $dummy2uploadframe->Button( -text => 'Stop', -command => \&dummy2uploa +dstop )->pack( -side =>'left' ); $dummy2uploadstatus = $dummy2uploadframe->Label(-text => $dummy2upload +value, -width => 40, -font=>"Verdana 10 bold")->pack( -side => 'left' + ); $mw->MainLoop; ################################################################### sub dummy1downloadstart { $dummy1downloadgo = 1; $timer = $mw->repeat(1, sub { $dummy1downloadstatus->configure(-text => $dummy1downloadvalue); $dummy1downloadstatus->update; $mw->update;}); } sub dummy1downloadstop { $timer->cancel; $dummy1downloadvalue = "IDLE"; $dummy1downloadstatus->configure(-text => $dummy1downloadvalue); $dummy1downloadstatus->update; $dummy1downloadgo = 0; } sub dummy1uploadstart { $dummy1uploadgo = 1; $timer = $mw->repeat(1, sub { $dummy1uploadstatus->configure(-text => $dummy1uploadvalue); $dummy1uploadstatus->update; $mw->update;}); } sub dummy1uploadstop { $timer->cancel; $dummy1uploadvalue = "IDLE"; $dummy1uploadstatus->configure(-text => $dummy1uploadvalue); $dummy1uploadstatus->update; $dummy1uploadgo = 0; } ##### dummy2 DOWNLOAD START ##### sub dummy2downloadstart { $dummy2downloadgo = 1; $timer = $mw->repeat(1, sub { $dummy2downloadstatus->configure(-text => $dummy2downloadvalue); $dummy2downloadstatus->update; $mw->update;}); } ##### dummy2 DOWNLOAD STOP ##### sub dummy2downloadstop { $timer->cancel; $dummy2downloadvalue = "IDLE"; $dummy2downloadstatus->configure(-text => $dummy2downloadvalue); $dummy2downloadstatus->update; $dummy2downloadgo = 0; } ##### dummy2 UPLOAD START ##### sub dummy2uploadstart { $dummy2uploadgo = 1; $timer = $mw->repeat(1, sub { $dummy2uploadstatus->configure(-text => $dummy2uploadvalue); $dummy2uploadstatus->update; $mw->update;}); } ##### dummy2 UPLOAD STOP ##### sub dummy2uploadstop { $timer->cancel; $dummy2uploadvalue = "IDLE"; $dummy2uploadstatus->configure(-text => $dummy2uploadvalue); $dummy2uploadstatus->update; $dummy2uploadgo = 0; } ##### dummy3 DOWNLOAD START ##### sub dummy3downloadstart { $dummy3downloadgo = 1; $timer = $mw->repeat(1, sub { $dummy3downloadstatus->configure(-text => $dummy3downloadvalue); $dummy3downloadstatus->update; $mw->update;}); } ##### dummy3 DOWNLOAD STOP ##### sub dummy3downloadstop { $timer->cancel; $dummy3downloadvalue = "IDLE"; $dummy3downloadstatus->configure(-text => $dummy3downloadvalue); $dummy3downloadstatus->update; $dummy3downloadgo = 0; } ##### dummy3 UPLOAD START ##### sub dummy3uploadstart { $dummy3uploadgo = 1; $timer = $mw->repeat(1, sub { $dummy3uploadstatus->configure(-text => $dummy3uploadvalue); $dummy3uploadstatus->update; $mw->update;}); } ##### dummy3 UPLOAD STOP ##### sub dummy3uploadstop { $timer->cancel; $dummy3uploadvalue = "IDLE"; $dummy3uploadstatus->configure(-text => $dummy3uploadvalue); $dummy3uploadstatus->update; $dummy3uploadgo = 0; }

Kindly help me on this

Thanks in advance.

Replies are listed 'Best First'.
Re: Perl TK threads using while
by zentara (Cardinal) on Feb 06, 2012 at 10:02 UTC
    The same i wanted do for 21 accounts.

    Put your Tabbed Notebook into a Scrolled Pane, so that the 21 accounts won't get crushed. Then use a hash to store all the important values. See what I did in Tk-with-worker-threads for example, everthing is stored in hashes so you can loop thru everything in an orderly manner.


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: Perl TK threads using while
by locked_user sundialsvc4 (Abbot) on Feb 06, 2012 at 14:13 UTC

    First of all, I did not closely read your code.   But I did pick up on the word, “thread.”   You do not need, say, “21 threads” to do this ... or even two.   You simply need a timer, along with a way of tracking the status of each of those 21 accounts as of the last time the timer ran.   On each timer-pop, the (main) program, or as the case may be a single background thread that does nothing else, does whatever it needs to do for everyone in turn.

      You do not need, say, “21 threads” to do this ... or even two.

      He is better off putting the socket codes into threads. If he just uses Tk's fileevent, (which is basically just select), he would be time slicing 21 sockets with a single fileevent. If one socket has a big upload, it will block all the other sockets until it is done. Having separate threads is very important here.


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

        Hi zentara

        Thanks for your response. The examples which you have mentioned starts automatically, but i want to start the event after pressing the button same for stop.

        If i assigned to the button it gets hangged.

        And every 3 seconds i want to display the status of process like Download in Process, Watching the Folder, No file found/ if found, Process started for file no.1/ Copying the file to server, sending e-mail. So that user will know there is some processing happning in tool.

        Any help will be much appreciated

        Thanks in Advance.