My goal is to compute and set the color of every pixel in a 500x500 image. Placing the entire computation in a single call back leaves my machine unresponsive for fifteen to thirty seconds. The only solution that I could think of was to compute one row of pixels at a time. Here is a demo of my implementation:

use strict; use warnings; use v5.14; use Tk; my $mw = new MainWindow( -title => 'Event demo'); my $drawarea = $mw->Frame()->pack( -side => 'top', -fill => 'both' ); $mw->bind('<<RowDone>>' => \&next_row); my $p = $mw->Photo(-width=>500, -height=>500); my $canvas = $drawarea->Canvas( -relief => 'ridge', -width => 500, -height => 500, -borderwidth => 4 )->pack(); $canvas->bind('<<RowDone>>' => \&next_row); $canvas->createImage(0,0, -anchor=>'nw', image=>$p); my $plot = $mw->Button(-text=>'Plot', -command=>\&init) ->pack(-side=>'left'); MainLoop; # Callbacks sub init { our $y = 0; $mw->eventGenerate('<<RowDone>>'); return; } sub next_row { our $y; for my $x ( 0..499 ) { my $quality = long_computation($x, $y); $p->put(color($quality), '-to', $x, $y); } $canvas->update; if (++$y < 500) { $mw->eventGenerate('<<RowDone>>', -when => 'tail' ); } return; } # stubs for demo only sub color { return 'red' } sub long_computation { return 10; }

This works as intended, but brings up some new issues.

The current row number ($y) must be declared globally with 'our'. I would prefer to pass a lexical value from one iteration to the next through the event structure. I have not been able to find a way to do this.

There does not seem to be any way to cancel a calculation in progress. I am unable to cancel a <<RowDone>> event which is already scheduled (or soon will be).

This is very likely an X-Y problem. I am interested in better solutions to the original problem as well as improvements to my solution.

Bill

In reply to long computation in TK by BillKSmith

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.