Tcl 脚本之间的通信

Communication between Tcl scripts

我成功地重定向了由我的 GUI (tcl/tk) 调用的脚本的标准输出: exec [info nameofexecutable] jtag.tcl >@$file_id

Here's a description of my system. 现在,当我单击 "stop" 按钮时,我希望能够告诉 jtag.tcl 停止数据采集(处于无限循环中)。可以通过 exec 还是我应该使用 open 来代替?

exec 命令等到子进程完成后才将控制权完全返回给您(除非您 运行 在后台完全断开连接)。要保持控制,您需要打开管道:

# Not a read pipe since output is redirected
set pipe [open |[list [info nameofexecutable] jtag.tcl >@$file_id] "w"]

您还需要确保其他进程在管道关闭时侦听,或者有一些其他协议来告诉另一端完成。最简单的机制是让远程端将管道(即它的stdin)置于非阻塞模式并定期检查退出消息。

# Putting the pipe into nonblocking mode
fconfigure stdin -blocking 0
# Testing for a quit message; put this in somewhere it can be called periodically
if {[gets stdin] eq "quit"} {
    exit
}

然后子进程的关闭协议在父进程中变成这样:

puts $pipe "quit"
close $pipe

或者,终止子进程并获取结果:

exec kill [pid $pipe]
# Need the catch; this will throw an error otherwise because of the signal
catch {close $pipe}