更新时间:1970-01-01 07:54:36
正如@ glenn-jackman指出的那样,首选使用fileevent(因为它应可在任何地方使用).
As @glenn-jackman pointed out, the use of fileevent is preferred (because it should work everywhere).
proc handle_bgexec {callback chan} {
append ::bgexec_data($chan) [read $chan]
if {[eof $chan]} {
# end of file, call the callback
{*}$callback $::bgexec_data($chan)
unset ::bgexec_data($chan)
}
}
proc bgexec {callback args} {
set chan [open "| $args" r]
fconfigure $chan -blocking false
fileevent $chan readable [list handle_bgexec $callback $chan]
return
}
将其调用为bgexec job_done cmd /c start /wait cmd /c make all-all
.完成命令后,job_done
被命令的输出调用.
Invoke this as bgexec job_done cmd /c start /wait cmd /c make all-all
. job_done
gets called with the output of the command after it finishes.
也可以使用线程来执行此操作,但这需要线程化的tcl构建(现在所有平台AFAIK都默认使用该线程,但是unix下的Tcl esp.的较早版本默认情况下不构建线程化的Tcl. )和Thread
软件包(默认包含).与线程一起使用的方法是:
It is also possible to use threads for this things, but this requires a threaded tcl build (which is now default for all platforms AFAIK, but older versions of Tcl esp. under unix don't build a threaded Tcl by default.) and the Thread
package (which is included by default). An approach to use it with Threads would be:
thread::create "[list exec cmd /c start /wait cmd /c make all-all];[list thread::send [thread::id] {callback code}];thread::exit"
如果您需要定期调用此线程,则可能只使用一个工作线程,而不是为每个作业创建一个新的线程.
If you need to call this on a regular basis it might be worth to use only one worker thread instead of creating a new one for each job.
添加/wait
作为参数以启动保持第一个cmd运行.
Add /wait
as parameter for start the keep the first cmd running.
cmd /c start /wait cmd /c make all-all