Running multiple instances of one tcl script

北战南征 提交于 2019-12-26 03:33:12

问题


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?


回答1:


There are several options to archive this:

1. Execute the script for each hostname:

foreach hostname $hosts {
    exec log.tcl $hostname &
}

This is like the bash solution.

2. Use threads

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.

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.



来源:https://stackoverflow.com/questions/28950548/running-multiple-instances-of-one-tcl-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!