如何使用2个子进程将许多字符串发送到子进程?

时间:2019-05-13 07:49:31

标签: python command-line command subprocess pipe

为了有效地索引Elasticsearch中的数据,我通过cURL发送内容,首先是编写命令文件,然后通过批量API发送

但是我认为我可以通过直接将内容发送到子流程来避免写时间:

jq = Popen("jq -c '. | {\"index\":{}}, . '" , shell=True, stdout=PIPE, stdin=PIPE)
curl = Popen("curl -S -H \"Content-type:application/x-ndjson\" -XPOST localhost:9200/myindex/_bulk/ —data-binary @-" , 
              shell=True, stdout=PIPE, stdin=jq.stdout)

for line in fic:
    entry = linetoentry(line) # make dict object 
    jq.stdin.write(json.dumps(entry).encode("utf-8"))

但是我得到一个erno 32 broken pipe,我读到它可能是因为第二个子进程没有从第一个子进程读取,所以它的“完整”,这种方式适用于较小的输入数据,但不适用于较大的输入数据,因此它是连贯的

该如何解决?这是向流程输入很多行的好方法吗?

0 个答案:

没有答案
相关问题