shell - 从 TCL 脚本在后台运行命令并格式化输出

标签 shell redirect background tcl

我有一个 tcl 脚本,它串行运行多个 shell 命令。

像这样的东西:

ABC文件

command 1
command 2 
command 3
...
command n

此脚本将这些命令的输出打印到以下格式的文本文件中:
### ### ### ### ### ###
Command name
### ### ### ### ### ###

Command Output

### ### ### ### ### ##

我试图让脚本运行得更快,但让 shell 命令并行而不是串行运行。通过将它们推到后台(命令 a &)。但是我不知道如何像以前一样保留我的输出文本文件的格式。

当我将命令推送到后台时,我被迫将它们的输出附加到一个临时文件中,但这些文件只是将命令的输出一起放在转储中。很难区分不同的输出。

有什么办法可以将在后台运行的每个命令的输出重定向到一个单独的临时文件(也许临时文件的名称可以具有后台运行进程的进程 ID)。一旦所有命令都运行完毕,我可以将输出组合成正确的格式吗?关于我如何做到这一点的任何想法/建议。

最佳答案

如果命令没有相互依赖的状态,您可以将它们并行化。有很多方法可以做到这一点,但一种更简单的方法是使用线程包的 thread pooling (这需要线程 Tcl,这是当今许多平台上的规范):

package require Thread

set pool [tpool::create -maxworkers 4]
# The list of *scripts* to evaluate
set tasks {
    {command 1}
    {command 2}
    ...
    {command n}
}

# Post the work items (scripts to run)
foreach task $tasks {
    lappend jobs [tpool::post $pool $task]
}

# Wait for all the jobs to finish
for {set running $jobs} {[llength $running]} {} {
    tpool::wait $pool $running running
}

# Get the results; you might want a different way to print the results...
foreach task $tasks job $jobs {
    set jobResult [tpool::get $pool $job]
    puts "TASK: $task"
    puts "RESULT: $jobResult"
}
主要可调整的是线程池的大小,默认限制为 4。(通过 -maxworkers 选项将其设置为 tpool::create,我已经在上面明确列出。)选择的最佳值取决于有多少您拥有的 CPU 内核以及每个任务平均产生多少 CPU 负载;你需要测量和调整……
您也可以使用 -initcmd使用您选择的脚本预加载池中的每个工作线程的选项。这是放置您的 package require 的好地方调用。 worker 彼此完全独立,也完全独立于主线程;他们不共享状态。如果您在单独的进程中运行每段代码,您将获得相同的模型(但您最终会编写更多代码来进行协调)。

[编辑]:这是一个适用于 Tcl 8.4 并使用子进程的版本。
namespace eval background {}
proc background::task {script callback} {
    set f [open |[list [info nameofexecutable]] "r+"]
    fconfigure $f -buffering line
    puts $f [list set script $script]
    puts $f {fconfigure stdout -buffering line}
    puts $f {puts [list [catch $script msg] $msg]; exit}
    fileevent $f readable [list background::handle $f $script $callback]
}
proc background::handle {f script callback} {
    foreach {code msg} [read $f] break
    catch {close $f}
    uplevel "#0" $callback [list $script $code $msg]
}

proc accumulate {script code msg} {
    puts "#### COMMANDS\n$script"
    puts "#### CODE\n$code"
    puts "#### RESULT\n$msg"

    # Some simple code to collect the results
    if {[llength [lappend ::accumulator $msg]] == 3} {
        set ::done yes
    }
}
foreach task {
    {after 1000;subst hi1}
    {after 2000;subst hi2}
    {after 3000;subst hi3}
} {
    background::task $task accumulate
}
puts "WAITING FOR TASKS..."
vwait done
注意:任务是产生结果的Tcl命令,但不能将结果打印出来;结构代码(在 background::task 中)处理。这些是子流程;它们彼此之间不共享任何内容,因此您希望它们执行或配置的任何内容都必须作为任务的一部分发送。一个更复杂的版本可以保留一个子进程的热池,并且通常工作得非常像线程池(由于处于子进程而不是线程中而存在细微差异),但这比我想在这里写的代码多.
结果代码(即异常代码)为 0 表示“ok”,1 表示“error”,其他值在不太常见的情况下。它们正是 Tcl 8.6 catch manual page 上记录的值;由您来正确解释它们。 (我想我还应该添加代码以使 ::errorInfo::errorCode 变量内容在出现错误的情况下被报告回来,但这会使代码变得更加复杂......)

关于shell - 从 TCL 脚本在后台运行命令并格式化输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13081383/

相关文章:

linux - CentOS 7 pidof -x 不工作

linux - 计算奇数文件大小的脚本

http - Golang http 重定向奇怪的行为

.htaccess 删除尾部斜杠

ruby-on-rails - 为什么 Rails 重定向我的 POST 而不是 GET?

css - 了解 css 背景速记属性

android - 使用背景图像刷新 android 中的 ImageView

linux - Shell 脚本 - 不要等待读取

android - 在recyclerview中更改所选项目的背景颜色

python - 使用 bash 或 python 将路由器配置文件转换为 csv