Python 与 grep 一起使用时子进程调用卡住 |
Python subprocess call stuck when used with grep |
我想获取大于 4GB 的 tar.gz 文件的未压缩文件大小。我找到了一个 shell 命令来做同样的事情,并且 shell 命令工作得很好。但是当我在我的 python 程序中使用相同的命令时,它永远不会完成。
我是运行 RHEL 6.8 上的脚本
获取正确未压缩文件大小的命令
gzip -dc some_tar_gz.tar.gz | wc -c
我的python脚本
import subprocess
import shlex
from pprint import pprint
command_list = shlex.split("gzip -dc some_tar_gz.tar.gz | wc -c")
result = subprocess.Popen(command_list, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
out, err = result.communicate()
pprint(out)
上面的 gzip 命令 return在 5 分钟内压缩了未压缩的文件大小。
但是上面的 python 脚本即使在 1 小时后 return 也没有任何结果。
编辑 1:
当我删除 shell=True
并看到 top
命令的结果时 python 进程占用了大约 27GB VIRT,之后进程被自动终止。我遇到了问题,但我不知道如何解决。
工作代码以防有人有同样的问题
import subprocess
import shlex
from pprint import pprint
command_list_1 = shlex.split("gzip -dc some_tar_file.tar.gz")
command_list_2 = shlex.split("wc -c")
p1 = subprocess.Popen(command_list_1, stdout=subprocess.PIPE)
p2 = subprocess.Popen(command_list_2, stdin=p1.stdout, stdout=subprocess.PIPE)
p1.stdout.close()
output = p2.communicate()[0]
pprint(output.rstrip())
我想获取大于 4GB 的 tar.gz 文件的未压缩文件大小。我找到了一个 shell 命令来做同样的事情,并且 shell 命令工作得很好。但是当我在我的 python 程序中使用相同的命令时,它永远不会完成。
我是运行 RHEL 6.8 上的脚本
获取正确未压缩文件大小的命令
gzip -dc some_tar_gz.tar.gz | wc -c
我的python脚本
import subprocess
import shlex
from pprint import pprint
command_list = shlex.split("gzip -dc some_tar_gz.tar.gz | wc -c")
result = subprocess.Popen(command_list, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
out, err = result.communicate()
pprint(out)
上面的 gzip 命令 return在 5 分钟内压缩了未压缩的文件大小。 但是上面的 python 脚本即使在 1 小时后 return 也没有任何结果。
编辑 1:
当我删除 shell=True
并看到 top
命令的结果时 python 进程占用了大约 27GB VIRT,之后进程被自动终止。我遇到了问题,但我不知道如何解决。
工作代码以防有人有同样的问题
import subprocess
import shlex
from pprint import pprint
command_list_1 = shlex.split("gzip -dc some_tar_file.tar.gz")
command_list_2 = shlex.split("wc -c")
p1 = subprocess.Popen(command_list_1, stdout=subprocess.PIPE)
p2 = subprocess.Popen(command_list_2, stdin=p1.stdout, stdout=subprocess.PIPE)
p1.stdout.close()
output = p2.communicate()[0]
pprint(output.rstrip())