我需要在Tcl中的线程之间进行双向通信,而我所能得到的只是将参数作为我唯一的master-> helper通信通道传入的一种方式。这就是我所拥有的:

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

...在循环中。

哪个不太好。而不是双向的。

任何人都知道2个线程之间的Windows中tcl的简单双向通信方法吗?

有帮助吗?

解决方案

这是一个小例子,展示了两个进程如何进行通信。首先关闭子进程(将其保存为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具有线程扩展,允许正确的线程间通信。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top