java - 如何多次发球到单个文件和程序

标签 java bash parallel-processing pipe tee

背景

我将以下命令作为 shell 脚本存储在 execution.sh

cat 输入文件 |三通输出文件 | java程序

我使用./execution.sh &成功地从input_file读取,将数据存储在单个output_file中,并作为输入发送到java程序。

问题

我想将input_file中的数据多次输出到output_file以及java程序。

例如并行读取相同的 input_file 5 次,并将数据发送到单个 output_file 和单个 java 程序

编辑

尝试解决方案

execution.sh

{
 python2 readLines.py &
 python2 readLines.py &
 python2 readLines.py &
 python2 readLines.py &
 python2 readLines.py &
} | tee  output_file | java program 

readLines.py

with open('inputfile') as f:
    for line in f:
       print line

我目前正在使用这个,如果有人看到任何问题,例如竞争条件等,请发表评论。

最佳答案

Reading same input_file say 5 times in parallel and send the data to a single output_file and single java program

让我们忽略“并行”部分。写入只能按顺序进行。

( for i in {1..5}; do cat input_file; done ) | tee out_file | java program

或者简而言之

cat input_file{,,,,} | tee out_file | java program

这两个命令连续打印文件 5 次。

如果您确实想并行编写,您可以启动cat命令作为后台作业:

( for i in {1..5}; do cat input_file & done ) | tee out_file | java program

此方法保证 output_file 包含 input_file 中的所有字节恰好五次,但(当然)是交错的。很有可能不仅是行,而且字节也最终会交错。这是什么意思?

如果您有该文件

abc
xyz

并行打印两次,输出可能会变成

ababcc

xxyz
yz

如果这不困扰你,还请记住,如果字节序列没有出现在该序列中,它们的含义就会丢失/改变,例如 Windows 新行 \r\n 或多字节 unicode 字符。

关于java - 如何多次发球到单个文件和程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42845855/

相关文章:

java - 通过更改替换数组的元素

java - 使用 onCreateView 中的功能动态更改 View

python - 如何知道输出是否进入终端?

bash - 使用带分隔符的 AWK 打印特定列

c++ - MPI:进程 0 执行其代码两次

parallel-processing - CUDA内核如何启动?

java - Java实际上是否并行运行线程

Java:如何在 JFrame 中绘制矩形?

java - Class.getResource() 在我的 Eclipse 应用程序中返回 null?无法配置类路径?

linux - 如何找出2个文件之间的差异