TCL:Windows中线程间的双向通信

TCL:Windows中线程间的双向通信,tcl,Tcl,我需要在Tcl中的线程之间进行双向通信,我所能得到的只是一种方式,参数作为我唯一的主->助手通信通道传入。以下是我所拥有的: proc ExecProgram { command } { if { [catch {open "| $command" RDWR} fd ] } { # # Failed, return error indication # error "$fd" } } 调用tclsh83,例如Exe

我需要在Tcl中的线程之间进行双向通信,我所能得到的只是一种方式,参数作为我唯一的主->助手通信通道传入。以下是我所拥有的:

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中tcl在两个线程之间的简单双向通信方法吗?

下面是一个小示例,演示了两个进程如何通信。首先是子进程(另存为child.tcl):

然后是启动子进程并与其通信的父进程:

set fd [open "| tclsh child.tcl" r+]

puts $fd "This is a test"
flush $fd

gets $fd line
puts $line
父进程使用open返回的值向子进程发送数据或从子进程接收数据;要打开的r+参数将打开管道进行读写操作

由于管线上存在缓冲,需要冲洗;可以使用fconfigure命令将其更改为行缓冲

还有一点;看看你的代码,你没有在这里使用线程,你正在启动一个子进程。Tcl有一个线程扩展,它允许正确的线程间通信

set fd [open "| tclsh child.tcl" r+]

puts $fd "This is a test"
flush $fd

gets $fd line
puts $line