Blocking with vwait and event loop
- From: "chad.ccdrbrg@xxxxxxxxx" <chad.ccdrbrg@xxxxxxxxx>
- Date: 2 May 2006 21:22:04 -0700
I am writing a small parent-child pipe based IPC routine.
I've adopted the common approach of using "puts" to send a request and
then waiting for a response using fileevent and vwait.
What I have works as desired with tclsh.
With Tk however, if a invoke my proc from a button press it hangs
(stdin block) after processing the -command method (before resuming
from the vwait command). This is in interactive mode.
If I use the button invoke method it works fine.
I'm assuming its has something to do with the internal coordination
of interpreter input and the event loop.
If I set stdin to non-blocking I get the desired behavior, but I'm
concerned about unexpected consequences.
My question is to those experienced with the internal workings of tcl;
can you explain what is causing this behavior and any suggestions
to correct it.
The code is copied below.
The main file ("main.tcl") defines the myCommand proc which initiates
communication by sending a request via "puts" and then waits for a
response.
The response is then forwarded to the "proc" _myCommand for processing.
This code would be the parent and I'm using stdin/stdout to "manually"
represent the child.
A second file ("gui.tcl") starts a GUI consisting of a single button
to invoke myCommand.
Session Example:
$ tclsh
% source main.tcl
% myCommand Hello
myCommand Hello
(type) This is a response
myCommand: line input = "This is a response"
% source gui.tcl
% .b1 invoke
(... works the same)
(click) button 1 "myCommand" (hang)
<file 1: main.tcl>
proc myCommand { args } {
puts "myCommand ${args}";
run _myCommand;
};
proc run { handler } {
set ::line {};
fileevent stdin readable [list wait ${handler}];
vwait ::done;
unset ::line
unset ::done
fileevent stdin readable {};
};
proc wait { handler } {
append ::line [gets stdin]
if [eof stdin] {
exit;
} else {
if { [regsub {\\$} ${::line} "" ::line] == 0 } {
${handler} ${::line};
set ::done 1;
};
};
};
proc _myCommand { string } {
puts "myCommand: line input = \"${string}\"";
#...
};
.
- Follow-Ups:
- Re: Blocking with vwait and event loop
- From: chad.ccdrbrg@xxxxxxxxx
- Re: Blocking with vwait and event loop
- Prev by Date: Re: is there a way to pause Tcl ?
- Next by Date: Re: Executing another tk file from a menubutton
- Previous by thread: GUI Design question
- Next by thread: Re: Blocking with vwait and event loop
- Index(es):