in reply to Please suggest a non-forking way to do this (OS: windows)

Seems to me that the problem here is that you are trying to use tie *STDOUT, 'Tk::Text', $widget; in ways it simply wasn't designed to work.

Although a brief scan didn't turn up any docs for tieing TK widgets in this way, it seems pretty likely that it is intended to take the output from a separate process and pipe it into the tied widget. And under win32, with fork being just an emulation using a thread, and async being a thread, what you're actually trying to do is pipe what is written to STDOUT by one thread and read it back from another thread.

STDOUT is process global--ie. shared by all threads in the process, although different threads my have different cloned/duped handles to it--which means that you would (at least) need to some kind of synchronisation between the threads. But I see no way of providing that without digging deep into the guts of the Tk widget/tie mechanism, which from past experience is a distinctly non-trivial undertaking.

The idea of writing from your process, into a piece of system allocated memory from one thread and then reading back from that system allocated memory in another thread and expecting the "system" to successfully mediate that is just a tad optimistic :)

You have a couple of options,

  1. Run your 'program' as a true separate process, (per ikegami's post), but using Win32::Process so that you can have the child process inherit the parent (Tk) process' standard handles.

    That might allow the tieing of STDOUT to a text widget to work?

  2. Run your subroutine in a thread, but modify it to write to a Thread::Queue instead of STDOUT. Then set-up a Tk::after repeating timer to read from that queue and write to the text widget.

    This keeps all the Tk interaction firmly in a single (main) thread. It works! And there are several examples of it kicking around this site.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

Replies are listed 'Best First'.
Re^2: Please suggest a non-forking way to do this (OS: windows)
by ikegami (Patriarch) on Sep 29, 2008 at 21:23 UTC

    That might allow the tieing of STDOUT to a text widget to work?

    You can't pass a tied handle to spawned process.

    Run your subroutine in a thread, but modify it to write to a Thread::Queue instead of STDOUT. Then set-up a Tk::after repeating timer to read from that queue and write to the text widget.

    That's the only solution I can think of.

    Modifying the subroutine is not technically necessary. STDOUT could be tied to a module which adds to a Thread::Queue similarly to how it's currently being tied to a module that adds to the Text widget.

      You can't pass a tied handle to spawned process.

      A spawned process can inherit (system level) handles from it's parent. In the same way that *nix can set up pipes in the parent and arrange for them to be inherited by the forked process so that it's STDOUT is connected to it's parent STDIN and vice versa, so you can do the same thing using CreateProcess.

      I can't find an documentation on this tie a handle to a widget, but it can't be entirely dissimilar under the covers. Something along the lines of this code posted a little while ago (I think an anonymonk or maybe it was tye?):

      use strict; use warnings; my $outfile = 'ff.tmp'; my $exe = $^X; my @args = ( 'perl', '-e', 'print qq{to stdout};print STDERR qq{to stderr}' ); print "here we go, running '$exe'\n"; print STDERR "here we go to stderr\n"; open(SAVOUT, ">&STDOUT") or die "error: save original STDOUT: $!"; open(SAVERR, ">&STDERR") or die "error: save original STDERR: $!"; open(STDOUT, '>', $outfile) or die "error: create '$outfile': $!"; open(STDERR, '>&STDOUT') or die "error: redirect '$outfile': $!"; system { $exe } @args; my $rc = $? >> 8; open(STDOUT, ">&SAVOUT") or die "error: restore STDOUT: $!"; open(STDERR, ">&SAVERR") or die "error: restore STDERR: $!"; close(SAVERR) or die "error: close SAVERR: $!"; close(SAVOUT) or die "error: close SAVOUT: $!"; print "rc=$rc\n"; print STDERR "rc=$rc to STDERR\n";

      Basically save the standard handle(s), connect it(them) to pipe(s), spawn the child with inheritance, restore the parent standard handle(s), write to/read from the pipes. It would be the parents end of the pipe connected to the childs STDOUT that you would then tie to the widget.

      A simplified version which might work (but haven't had time to try), is to spawn the child using a piped open and then tie the pipe to the widget.

      STDOUT could be tied to a module which adds to a Thread::Queue similarly to how it's currently being tied to a module that adds to the Text widget.

      That's an interesting idea. Code could be added to Thread::Queue that inspects the mode supplied on the tie (read or write) and then tie the appropriate ends of the queue to a tied handle.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        A spawned process can inherit (system level) handles from it's parent.

        Agreed. I didn't say otherwise.

        It would be the parents end of the pipe connected to the childs STDOUT that you would then tie to the widget.

        It's either tied or the "parent's end of the pipe". It can't be both.

Re^2: Please suggest a non-forking way to do this (OS: windows)
by cranky (Novice) on Oct 01, 2008 at 14:02 UTC
    Thanx for all the help guys, sorry for the late reply...
    I'm trying with the second method:

    Run your subroutine in a thread, but modify it to write to a
    Thread::Queue instead of STDOUT. Then set-up a
    Tk::after repeating timer to read from that queue and write to the text widget.

    Will get back soon