Python子进程管道挂着沟通()

时间:2012-09-05 22:10:53

标签: python subprocess

我在使用subprocess模块的Python中看起来像一段简单的代码时遇到了一些问题。

我的目标是我有gzip格式的文件,我想读取该文件,解压缩并通过其他外部实用程序运行它,将其插入数据库。

出于某种原因,每当我在第二个子进程上调用communicate()方法时,我的整个进程将永远挂起,并且没有任何内容插入到我的数据库中。我肯定对管道,标准杆和标准做错了,但是不能弄清楚是什么,所以任何帮助都会受到赞赏。

我尝试过相同的过程,文件根本没有压缩,只有一个子进程调用外部实用程序,但它的效果很好。

from subprocess import Popen, PIPE

cmd_load_resolved = "nzload -host <host> -u <user> -pw <password> -db <database> -t <table>"
cmd = Popen(["gunzip"], stdin=PIPE, stdout=PIPE)
nz = Popen(cmd_load_resolved.strip().split(" "), stdin=cmd.stdout, stdout=PIPE, stderr=PIPE)
for raw in file_key:
    cmd.stdin.write(raw)
cmd.stdin.close() # not sure if needed, i tried without but doesn't change anything
cmd.stdout.close()
nz.communicate() # hangs forever

有什么想法吗?

0 个答案:

没有答案
相关问题