如何并行执行命令运行

how can parallel exec command running

我有这个脚本,想运行并行执行。现在是运行一个接一个。我该怎么做?

非常感谢您的帮助

bind pub -|- !tt proc:tt
proc proc:tt {nick host handle channel arg} {

set search [lindex [split [stripcodes bcu $arg]] 0]

foreach name {name1 name2 name3 name4 name5} {
    set results [exec sh search.sh $name $search]
putnow "PRIVMSG $channel :results: $results"
}
}

目前问题陆续出现。但应该是平行的

[24.02.20/22:00:59] <testbot> results: /home/test/name1
[24.02.20/22:01:34] <testbot> results: /home/test/name2
[24.02.20/22:03:05] <testbot> results: /home/test/name3
[24.02.20/22:09:05] <testbot> results: /home/test/name4
[24.02.20/22:09:07] <testbot> results: /home/test/name5

https://tcl.tk/man/tcl8.6/TclCmd/exec.htm

If the last arg is & then the pipeline will be executed in background. In this case the exec command will return a list whose elements are the process identifiers for all of the subprocesses in the pipeline. The standard output from the last command in the pipeline will go to the application's standard output if it has not been redirected, and error output from all of the commands in the pipeline will go to the application's standard error file unless redirected.

要在后台运行一个命令并捕获结果,你需要异步打开一个管道和运行东西。 (如果 & 是它的最后一个参数,exec 命令可以在后台 运行 事情,但在捕获结果时不会。)

bind pub -|- !tt proc:tt
proc proc:tt {nick host handle channel arg} {
    set search [lindex [split [stripcodes bcu $arg]] 0]

    foreach name {name1 name2 name3 name4 name5} {
        # Launch the subprocess
        set pipe [open |[list sh search.sh $name $search]]
        # Set up a handler for when the subprocess produces *any* output *or* finishes
        fconfigure $pipe readable [list handle:search:output $channel $pipe $name]
    }
    putnow "PRIVMSG $channel processing..."
}
proc handle:search:output {channel pipe name} {
    set line [gets $pipe]
    if {[eof $pipe]} {
        close $pipe
        putnow "PRIVMSG $channel search for $name done"
        return
    }
    putnow "PRIVMSG $channel :result($name): $line"
}

请注意,这会并行启动所有搜索。这可能会给您的系统带来很大的负担!这也可能以任意顺序提供结果。按顺序 做事 是可能的,但需要更复杂的代码和手动继续传递,或协程(Tcl 8.6 或更高版本)。或者您可以将执行的事情按顺序交给子流程:这很简单。