代码之家  ›  专栏  ›  技术社区  ›  Dams0622

Tcl脚本之间的通信

  •  0
  • Dams0622  · 技术社区  · 7 年前

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

    Here's a description of my system. 现在我想告诉jtag。当我点击“停止”按钮时,tcl停止数据采集(在无限循环中)。可以通过吗 exec 或者我应该使用 open 相反

    1 回复  |  直到 7 年前
        1
  •  1
  •   Donal Fellows    7 年前

    这个 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}