在多个线程/进程之间共享标准输出 [英] Sharing stdout among multiple threads/processes

查看:130
本文介绍了在多个线程/进程之间共享标准输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Linux程序(语言无关紧要),它将程序的日志打印到stdout上. 监视过程所需的日志IS.

I have a linux program(the language doesn't matter) which prints it's log onto stdout. The log IS needed for monitoring of the process.

现在,我将通过派生或使用线程来对其进行并行化.

Now I'm going to parallelize it by fork'ing or using threads.

问题:生成的标准输出将包含不相关行的不可读混合...

最后是问题:您将如何为并行进程重建输出逻辑?

推荐答案

很抱歉回答自己...

Sorry for answering myself...

确定的解决方案是使用 GNU parallel 实用工具.

The definite solution was to use the GNU parallel utility.

它取代了众所周知的xargs实用程序,但是并行运行命令,将输出分成几组.

It came to replace the well known xargs utility, but runs the commands in parallel, separating the output into groups.

所以我只是按原样保留了我的简单的单进程,单线程实用程序,并通过parallel通过管道将其调用通过管道发送,如下所示:

So I just left my simple one-process, one-thread utility as-is and piped its call through the parallel like that:

generate-argument-list |平行<选项>我的实用程序

generate-argument-list | parallel < options > my-utility

这取决于并行选项,可以为多次调用my-utility

This, depending on parallel's options can produce nicely grouped outputs for multiple calls of my-utility

这篇关于在多个线程/进程之间共享标准输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆