简体   繁体   English

多个程序可以同时写入STDOUT吗?

[英]Can multiple programs write to STDOUT at the same time?

I'm currently using GNU Parallel to run a Python script on multiple large files simultaneously. 我目前正在使用GNU Parallel同时在多个大文件上运行Python脚本。 I have a master Python script that sets up the files I need to process, and then dispatches Parallel to run the same worker script on these files. 我有一个主Python脚本,用于设置我需要处理的文件,然后调度Parallel以在这些文件上运行相同的工作脚本。

I need to get the data back into the master thread when all the workers have finished, and I'm currently just writing to files and pulling them back out. 当所有工作人员完成时,我需要将数据恢复到主线程中,而我现在只是写入文件并将其拉回来。 Is it possible for the workers to dump their pickled contents onto STDOUT for the master to collect, or is there a chance that two workers could start writing to STDOUT at the same time and interleave with each other? 工人是否有可能将他们的腌制内容转移到STDOUT上供主人收集,或者两个工人是否有可能同时开始写STDOUT并相互交错?

Basically, will STDOUT block until another program has finished? 基本上,STDOUT会阻塞直到另一个程序完成吗?

In a shell environment, starting various jobs in the background, all writing to stdout , has a high chance of interleaving that output, as there is no lock on stdout . 在shell环境中,在后台启动各种作业,所有写入stdout ,都很有可能交错输出,因为stdout没有锁定。

However, GNU Parallel can redirect stdout for the various jobs it starts and prevent this interleaving. 但是,GNU Parallel可以将stdout重定向到它启动的各种作业,并防止这种交错。 There are several commmand line switches and various options. 有几个命令行开关和各种选项。

By default output is grouped: 默认输出分组:

--group
Group output. 集团产出。 Output from each jobs is grouped together and is only printed when the command is finished. 每个作业的输出组合在一起,仅在命令完成时打印。 stderr (standard error) first followed by stdout (standard output). stderr(标准错误)首先跟着stdout(标准输出)。 This takes some CPU time. 这需要一些CPU时间。 In rare situations GNU parallel takes up lots of CPU time and if it is acceptable that the outputs from different commands are mixed together, then disabling grouping with -u can speedup GNU parallel by a factor of 10. 在极少数情况下,GNU并行占用大量CPU时间,如果可以接受来自不同命令的输出混合在一起,则使用-u禁用分组可以将GNU并行加速10倍。

--group is the default. --group是默认值。 Can be reversed with -u . 可以用-u反转。

But other options, including directing to files, are available. 但是可以使用其他选项,包括指向文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM