in reply to subprocess delay on Windows with Perl/Tk

Here is a version that runs just fine on Windows XP ... using threads.
use strict; use Tk; use File::Spec; use threads; use Thread::Queue; my $RequestQ = Thread::Queue->new; my $worker = threads->new(\&worker); my $top = MainWindow->new(); my $menubar = $top->Menu(-type => 'menubar'); $top->configure(-menu => $menubar); my $filemenu = $menubar->cascade(-label => '~File', -tearoff => 0); $filemenu->command(-label => '~Report extract', -command => [\&report_extract], ); $filemenu->command(-label => 'E~xit', -command => [\&Shutdown], ); print STDERR "there's a tiny window open, it's small but it's there\n" +; MainLoop(); print STDERR "Exit Main loop \n"; ############################ sub Shutdown{ $RequestQ->enqueue(undef); # quit the worker $worker->join; print STDERR "FINISHED Shutdown\n"; exit; # Clean up Tk } ############################ sub report_extract { my $tmpdir = File::Spec->tmpdir(); open (F, ">$tmpdir\\xx.csv") || die $!; print F "aaa,bbb,ccc\nddd,eee,fff\n"; close F; my $cmd = "start $tmpdir\\xx.csv"; print STDERR "running $cmd\n"; $RequestQ->enqueue($cmd); } ################## sub worker{ while (my $target = $RequestQ->dequeue){ print STDERR "Request $target Read\n"; system $target; print STDERR "Finished target\n"; } print STDERR "Thread exit\n"; }

Offense, like beauty, is in the eye of the beholder, and a fantasy.
By guaranteeing freedom of expression, the First Amendment also guarantees offense.

Replies are listed 'Best First'.
Re^2: subprocess delay on Windows with Perl/Tk
by kgoess (Beadle) on Jun 05, 2004 at 15:03 UTC
    Oooh! Now that's fancy. I like it. What was the train of thought that led you to consider threads as a solution? It never would have occurred to me.
      I had always found "fork" non-intutive, and a pain (on Win32), because you needed a mechanism to exchange data between parent and child - the working ones are complicated (to my simple mind).

      I had tested perl threads on Win32 and found that to be robust, intutive and, to my twisted brain, very logical.

      The thread queue model rules!!

      I had never tried using threads with Tk - so I enjoyed playing with your code.
      Cheers.

      Offense, like beauty, is in the eye of the beholder, and a fantasy.
      By guaranteeing freedom of expression, the First Amendment also guarantees offense.

        The nice, if slightly strange thing is that as fork is emulated using threads (win32), you can actually use Thread::Queue to communicate between forked pseudo-processes.

        #! perl -slw use strict; use threads; use Thread::Queue; $|=1; my $QpRead = new Thread::Queue; my $QkRead = new Thread::Queue; if( defined( my $kid=fork() ) ) { print 'Forked'; if( $kid ) { # Parent print 'Pop ', $$; while( 1 ) { print "Kid said: ", $QpRead->dequeue while $QpRead->pendin +g; Win32::Sleep rand 1000; $QkRead->enqueue( 'Blah ' x rand 10 ) if rand > .5; } } else { # Kid print 'Kid ', $$; while( 1 ) { print "Pop said: ", $QkRead->dequeue while $QkRead->pendin +g; Win32::Sleep rand 1000; $QpRead->enqueue( 'Blah ' x rand 10 ) if rand > .5; } } } else { die "Fork failed: $!"; } __END__ P:\test>357845 Forked Pop 416 Forked Kid -1140 Pop said: Blah Blah Blah Kid said: Blah Blah Blah Blah Kid said: Blah Kid said: Blah Blah Blah Blah Kid said: Blah Blah Blah Blah Blah Blah Blah Blah Kid said: Blah Blah Blah Blah Blah Pop said: Blah Blah Kid said: Blah Blah Blah Blah Blah Blah Blah Pop said: Blah Blah Blah Blah Blah Blah Pop said: Blah Blah Blah Blah Blah Blah Blah Pop said: Kid said: Blah Blah Blah Blah Pop said: Blah Blah Kid said: Blah Blah Blah Blah Blah Blah Blah Blah Kid said: Blah Blah Blah Blah Blah Blah Blah Blah Pop said: Blah Blah Blah Blah Blah Blah Pop said: Blah Blah Blah Blah Blah Blah Blah Blah Kid said: Blah Blah Blah Blah Blah Blah Blah Pop said: Blah Blah Blah Blah Blah Kid said: Blah Blah Blah Blah Blah Kid said: Blah Pop said: Blah Blah Blah Blah Blah Blah Blah Blah Kid said: Blah Blah Blah Blah Blah Blah Blah Pop said: Blah Kid said: Blah Blah Blah Blah Blah Pop said: Blah Blah Blah Kid said: Blah Terminating on signal SIGINT(2)

        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail