我需要在 Tcl 中的线程之间进行两种方式的通信,而我所能得到的只是一种将参数作为我唯一的主-> 辅助通信 channel 传入的方式。这是我所拥有的:
proc ExecProgram { command } {
if { [catch {open "| $command" RDWR} fd ] } {
#
# Failed, return error indication
#
error "$fd"
}
}
调用tclsh83,例如ExecProgram "tclsh83 testCases.tcl TestCase_01"
在 testCases.tcl 文件中,我可以使用传入的信息。例如:
set myTestCase [lindex $argv 0]
在 testCases.tcl 中,我可以输出到管道中:
puts "$myTestCase"
flush stdout
并使用进程 ID 接收放入主线程的内容:
gets $app line
...在一个循环中。
这不是很好。而且不是双向的。
任何人都知道 Windows 中 2 个线程之间的 tcl 的简单 2 路通信方法?
最佳答案
这是一个小例子,展示了两个进程如何通信。首先关闭子进程(将其保存为 child.tcl):
gets stdin line
puts [string toupper $line]
然后是启动子进程并与之通信的父进程:
set fd [open "| tclsh child.tcl" r+]
puts $fd "This is a test"
flush $fd
gets $fd line
puts $line
父进程使用 open 返回的值向子进程发送数据和从子进程接收数据;要打开的 r+ 参数为读取和写入打开管道。
由于管道上的缓冲,因此需要刷新;可以使用 fconfigure 命令将其更改为行缓冲。
还有一点;看看你的代码,你没有在这里使用线程,你正在启动一个子进程。 Tcl 有一个线程扩展,它允许正确的线程间通信。
关于TCL:Windows 中线程之间的双向通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/267420/