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

标签 python multithreading parallel-processing multiprocessing stdout

我目前正在使用 GNU Parallel 同时在多个大文件上运行 Python 脚本。我有一个主 Python 脚本,用于设置我需要处理的文件,然后调度 Parallel 在这些文件上运行相同的工作脚本。

当所有工作人员完成后,我需要将数据返回到主线程,而我目前只是写入文件并将它们拉出来。工作人员是否有可能将其腌制内容转储到 STDOUT 上供主设备收集,或者两个工作人员是否有可能同时开始写入 STDOUT 并相互交错?

基本上,STDOUT 会阻塞直到另一个程序完成吗?

最佳答案

在 shell 环境中,在后台启动各种作业,所有作业都写入 stdout,很有可能交错该输出,因为 stdout 上没有锁定.

但是,GNU Parallel 可以为它启动的各种作业重定向 stdout,并防止这种交错。有几个commmand line switches以及各种选项。

默认情况下输出是分组的:

--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). This takes some CPU time. 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.

--group is the default. Can be reversed with -u.

但也可以使用其他选项,包括定向到文件。

关于python - 多个程序可以同时写入 STDOUT 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28525048/

相关文章:

c# - 在新线程上使用 ObservableCollection

c# - C#-OutOfMemoryException将列表保存在JSON文件中

python - Joblib 并行多个 cpu 比单个慢

c# - 排序列表和并行 Linq 查询

python - 替换部分字符串

python - 为什么 pip 说 "No module named commands.install"?

java - 在多线程应用程序中,是否应该使用 ReadWriteLock 锁定基本类型的 getter/setter?

parallel-processing - GNU 并行 : suppress warning about increasing blocksize

Python Matplotlib : black squares when saving eps of plot of masked array, 为什么?

python - 如何检查PDB中当前调试函数的返回值?