您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

为什么python.subprocess在proc.communicate()之后挂起?

为什么python.subprocess在proc.communicate()之后挂起?

文档中获取communicate

与进程交互:将数据发送到stdin。从stdout和stderr读取数据,直到到达文件末尾。

因此,communicate()运行后,该过程已 。

如果您想在不等待过程停止的情况下进行读写:

不要使用shell=True-它不需要调用外壳程序来依次调用您的程序,因此您和您的程序之间将存在另一个进程。这有很多令人不快的副作用。认值为,shell=False因此您应该坚持使用。将Popen行更改为:

p = subprocess.Popen(["./AO_FelixStrategy_UnitTest",
                  "--bats", "31441", "--chix", "12467",
                  "--enxutp", "31884", "--turq", "26372",
                  "--symbol", "SOGN", "--target_date", '2009-Oct-16'],
                 stdin=subprocess.PIPE, 
                 stdout=subprocess.PIPE)

使用p.stdin.write写入过程。用于p.stdout.read从中读取。

调用p.stdout.read如果没有什么read将被阻塞。p.stdin.write如果写缓冲区已满,则调用将阻塞。因此,您必须确保您有一些要读/写的东西-您可以在Unix OS上使用来进行操作select。在Windows上,不幸的是您必须诉诸线程。至少那是Popen.communicate内部的。

根据您的描述,这是一些示例代码。它的工作取决于AO_FelixStrategy_UnitTest开发方式:

p = subprocess.Popen(["./AO_FelixStrategy_UnitTest",
                      "--bats", "31441", "--chix", "12467",
                      "--enxutp", "31884", "--turq", "26372",
                      "--symbol", "SOGN", "--target_date", '2009-Oct-16'],
                     stdin=subprocess.PIPE, 
                     stdout=subprocess.PIPE)
output = p.communicate('S\nL\n')[0]
print output
python 2022/1/1 18:28:45 有186人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶