【问题标题】:TCL: Two way communication between threads in WindowsTCL:Windows中线程之间的双向通信
【发布时间】:2008-11-06 01:44:20
【问题描述】:

我需要在 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

...在一个循环中。

这不是很好。而不是双向的。

有谁知道 Windows 中 2 个线程之间 tcl 的简单 2 路通信方法?

【问题讨论】:

    标签: tcl


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2021-11-10
      • 1970-01-01
      • 2017-12-24
      • 2014-05-24
      • 1970-01-01
      • 1970-01-01
      • 2020-03-18
      • 2013-02-11
      • 1970-01-01
      相关资源
      最近更新 更多