如何抑制 TCL 线程的错误/输出?

发布于 2025-01-10 21:27:59 字数 343 浏览 1 评论 0原文

我创建了一个线程:

set t1 [thread::create]
thread::send $t1 {
    proc myProc {command args} {
        exec {*}[auto_execok $command] {*}$args >& /dev/null
    }
}

然后尝试发送异步命令:

thread::send -async $t1 [list myProc <command args>]

但是命令的错误/输出显示在输出中。 如何隐藏发送到异步线程的命令的错误/输出?

I have created a thread :

set t1 [thread::create]
thread::send $t1 {
    proc myProc {command args} {
        exec {*}[auto_execok $command] {*}$args >& /dev/null
    }
}

And then tried to send an asynchronous command :

thread::send -async $t1 [list myProc <command args>]

But the error/output from the command is getting displayed in output.
How to hide the errors/outputs from the command that is send to the async thread ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云仙小弟 2025-01-17 21:27:59

最简单的方法是捕获错误。

thread::send $t1 {
    proc myProc {command args} {
        catch {
            exec {*}[auto_execok $command] {*}$args >& /dev/null
        }
    }
}

请注意,这会使任何问题显着更难以调试!如果您能够识别预期的错误并尝试捕获它们,那就更好了,因此意外错误仍然是您看到并必须处理的事情。

thread::send $t1 {
    proc myProc {command args} {
        try {
            exec {*}[auto_execok $command] {*}$args >& /dev/null
        } trap CHILDSTATUS {} {
            # Ignore a non-zero exit?
        }
    }
}

对于这种特定情况(从 Tcl 的角度来看,这是非常受 I/O 限制的),您最好将 & 添加到 exec 调用的末尾,而不是在单独的线程中运行。在不确切知道自己在做什么的情况下,无法确定,但值得考虑。

The simplest method is to catch the errors.

thread::send $t1 {
    proc myProc {command args} {
        catch {
            exec {*}[auto_execok $command] {*}$args >& /dev/null
        }
    }
}

Be aware that this makes any problems significantly more difficult to debug! It's better if you can identify what errors you expect and just try to catch them, so unexpected errors are still things you see and have to handle.

thread::send $t1 {
    proc myProc {command args} {
        try {
            exec {*}[auto_execok $command] {*}$args >& /dev/null
        } trap CHILDSTATUS {} {
            # Ignore a non-zero exit?
        }
    }
}

For this specific case (which is very much I/O-bound from Tcl's perspective) you might be better off just adding & to the end of the exec call and not running in a separate thread. Can't really say for sure without knowing exactly what you're doing, but it is worth considering.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文