Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用多处理时如何与Popen通信_Python_Subprocess_Multiprocessing_Python Multiprocessing - Fatal编程技术网

Python 使用多处理时如何与Popen通信

Python 使用多处理时如何与Popen通信,python,subprocess,multiprocessing,python-multiprocessing,Python,Subprocess,Multiprocessing,Python Multiprocessing,最初,我的代码是: if __name__ == '__main__': subproc = subprocess.Popen("fem.exe", stdin=subprocess.PIPE, stdout=subprocess.PIPE) subproc.stdin.write("1\n") subproc.stdin.write("1\n1\n1\n1\n1\n1\n") subproc.stdin.flush() while True:

最初,我的代码是:

if __name__ == '__main__':
    subproc = subprocess.Popen("fem.exe", stdin=subprocess.PIPE, stdout=subprocess.PIPE)
    subproc.stdin.write("1\n")
    subproc.stdin.write("1\n1\n1\n1\n1\n1\n")
    subproc.stdin.flush()
    while True:
        line = subproc.stdout.readline()
        print line
它将“fem.exe”调用为
subproc
,将输入发送到
subproc
,并通过管道返回输出。这段代码运行良好

但是,现在我需要从另一个线程发送一些输入

subproc = subprocess.Popen("fem.exe", stdin=subprocess.PIPE, stdout=subprocess.PIPE)
def run():
    subproc.stdin.write(get_rest_input())
    subproc.stdin.flush()

if __name__ == '__main__':
    subproc.stdin.write("1\n")
    process = multiprocessing.Process(target=run)
    process.start()
    while True:
        line = subproc.stdout.readline()
        print line
在这个版本中,我创建了一个新的
过程
,在这个过程中调用
get\u rest\u input()
来计算剩余的输入。然而,我从“fem.exe”中得到一个错误,它告诉我遇到了一个EOF错误

run-time error F6501: READ(CON)
- end of file encountered

我只是想知道为什么会发生这种情况,是因为我使用多处理吗?如果是这样,为什么我不能这样写,以及如何解决这个问题?

你没有理由为此使用多处理。使用多线程,您将能够共享变量。另外:永远不要
flush
stdin。如果你没有其他东西要写,就关闭它。@Jean-Françoisfare谢谢你,现在我知道实际上
子程序
有两个进程,所以我不能共享它们,当我试图将它们作为参数传递时,我得到了一个
pickle
错误。我只是想问一下,是否有相关的演练(如果我仍然想使用多处理)?因为我认为由于python的GILabout threads&performance,使用多线程将导致性能损失:对于纯python来说,这是正确的,但是如果其中一个线程忙于运行命令,则不会损失性能。