TCL:Windows 中线程之间的双向通信
我需要在 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 中 2 个线程之间 tcl 的简单 2 路通信方法吗?
I need to have two way communication between threads in Tcl and all I can get is one way with parameters passing in as my only master->helper communication channel. Here is what I have:
proc ExecProgram { command } {
if { [catch {open "| $command" RDWR} fd ] } {
#
# Failed, return error indication
#
error "$fd"
}
}
To call the tclsh83, for example ExecProgram "tclsh83 testCases.tcl TestCase_01"
Within the testCases.tcl file I can use that passed in information. For example:
set myTestCase [lindex $argv 0]
Within testCases.tcl I can puts out to the pipe:
puts "$myTestCase"
flush stdout
And receive that puts within the master thread by using the process ID:
gets $app line
...within a loop.
Which is not very good. And not two-way.
Anyone know of an easy 2-way communication method for tcl in Windows between 2 threads?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个小示例,展示了两个进程如何进行通信。 首先关闭子进程(将其保存为 child.tcl):
然后是启动子进程并与其通信的父进程:
父进程使用 open 返回的值向子进程发送和接收数据; r+ 参数 open 打开管道以进行读取和写入。
由于管道上有缓冲,因此需要刷新; 可以使用 fconfigure 命令将其更改为行缓冲。
还有一点; 查看您的代码,您在这里没有使用线程,您正在启动一个子进程。 Tcl 有一个线程扩展,它允许正确的线程间通信。
Here is a small example that shows how two processes can communicate. First off the child process (save this as child.tcl):
and then the parent process that starts the child and comunicates with it:
The parent uses the value returned by open to send and receive data to/from the child process; the r+ parameter to open opens the pipeline for both read and write.
The flush is required because of the buffering on the pipeline; it is possible to change this to line buffering using the fconfigure command.
Just one other point; looking at your code you aren't using threads here you are starting a child process. Tcl has a threading extension which does allow proper interthread communications.