运行一个tcl脚本的多个实例 [英] Running multiple instances of one tcl script
问题描述
当前,我正在使用foreach命令从文件中读取主机名,并使用该主机名连接到设备.所有这些都是在我的TCL脚本中完成的.有没有一种方法可以运行脚本的多个实例,以便分别查询每个设备?类似于bash脚本的东西,类似于:
Currently I am reading in host names from a file using a foreach command and using this hostname to connect to the device. All this is done within my TCL script. Is there a way I can run multiple instances of the script so that each device is queried separately? Something like a bash script similar to:
for hostname in file;
do
log.tcl $hostname &
done
我相信这类似于多线程. 我还有一个问题是,当运行一个脚本的多个实例,并且每个脚本写入同一文件时,是否会将所有日志弄乱了?
I believe this is similar to multi threading. Another question I have is that, when running multiple instances of a script, and each script writes to the same file, will all the logs be jumbled up?
推荐答案
可以通过以下几种方法进行存档:
There are several options to archive this:
foreach hostname $hosts {
exec log.tcl $hostname &
}
这就像bash解决方案.
This is like the bash solution.
package require Thread
set pool [tpool::create]
set jobs {}
foreach hostname $hosts {
lappend jobs [tpool::post -nowait $pool [list apply {{host} {
set argv0 log.tcl
set argv [list $host]
source $argv0
} $hostname]]
}
while {[llength $jobs]} {
tpool::wait $pool $jobs jobs
}
请注意,期望的问题不能很好地与线程.
Note that expect does not work nicely with threads.
关于从多个脚本写入同一文件的另一个问题:这取决于.如果您具有POSIX兼容系统,并使用a
打开文件,则可能会起作用.
For the other question regarding writing to the same file from multiple scripts: It depends. If you have a POSIX compliant system and open the files with a
, then it might work.
这篇关于运行一个tcl脚本的多个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!