in reply to long computation in TK
I am thrown off a bit by your use of generating custom events to signal a row done. Remember, Tk should only be used for the display of the result, don't use it to do calculations. Put the calculations in a forked process or thread, and pass it back to Tk for display. It would be very responsive to a binding to the q key to stop. The Tk event loop would use a timer or a fileevent to get the resultant image data back from the thread and update the display. I probably would use GD to do the pixel manipulation, with set_pixel, in the thread or fork. Do one line at a time and return the array. Then use Tk to display the accumulated results on the canvas. Be careful with creating a new image each time you need to update the canvas, you may get a memory leak. Instead, clear out the current image object's data, and refill it.
See Re: Tk photo display: memory never released (not leaking solution feeding -data to Tk::Photo)
Here is a basic example to use threads with Tk. Re^2: Tk-with-worker-threads. Notice Tk needs a timer to read the thread data. Alternatively, you could use a fileevent to read the filhandle from the thread directly.
#!/usr/bin/perl use warnings; use strict; use threads; use threads::shared; use Tk; my %shash; #share(%shash); #will work only for first level keys my %hash; share ($shash{'go'}); share ($shash{'fileno'}); share ($shash{'pid'}); share ($shash{'die'}); $shash{'go'} = 0; $shash{'fileno'} = -1; $shash{'pid'} = -1; $shash{'die'} = 0; $hash{'thread'} = threads->new(\&work); my $mw = MainWindow->new(-background => 'gray50'); my $text = $mw->Scrolled('Text')->pack(); my $startb = $mw->Button( -text => 'Start', -command=>sub{ $shash{'go'} = 1; $mw->after(100); #give pipe chance to startup my $fileno = $shash{'fileno'}; print "fileno_m $fileno\n"; open (my $fh, "<&=$fileno") or warn "$!\n"; # filevent works but may not work on win32, # but you can use a timer instead as shown below $mw->fileevent(\*$fh, 'readable', ); while(<$fh>){ $text->insert('end',$_); $text->see('end'); $mw->update; } # on Win32 (untested by me) you will need # a timer instead of fileevent # my $repeater; # $repeater = $mw->repeat(10, # sub { # my $bytes = sysread( "<&=$fileno", my $buf, 8192); # $text->insert('end',$buf); # $text->see('end'); # if( $shash{'go'} == 0 ){ $repeater->cancel } # } # ); } )->pack(); my $stopb = $mw->Button( -text => 'Stop/Exit', -command=>sub{ $shash{'die'} = 1; kill 9,$shash{'pid'}; $hash{'thread'}->join; exit; }, )->pack(); MainLoop; ################################################################## sub work{ $|++; while(1){ if($shash{'die'} == 1){ return }; if ( $shash{'go'} == 1 ){ #run your command here, and try to find a way to capture it's +output # on win32 you may need to use IPC::Run in the thread my $pid = open(FH, "top -b |" ) or warn "$!\n"; my $fileno = fileno(FH); print "fileno_t->$fileno\n"; $shash{'fileno'} = $fileno; $shash{'pid'} = $pid; $shash{'go'} = 0; #turn off self before returning }else { select(undef,undef,undef,.1) } #short sleep } } #####################################################################
Good luck.
|
|---|