Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.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使用子流程与其他应用程序对话_Python_Subprocess - Fatal编程技术网

Python使用子流程与其他应用程序对话

Python使用子流程与其他应用程序对话,python,subprocess,Python,Subprocess,这就是我的想法。我将使用“main”python脚本启动(使用子流程)app1和app2“main”脚本将向app1发送输入,并向app2输出结果,反之亦然(并且主脚本需要记住发送的内容,所以我无法将管道从app1发送到app2) 这是主脚本 import subprocess import time def main(): prvi = subprocess.Popen(['python', 'random1.py'], stdin = subprocess.PIPE , stdou

这就是我的想法。我将使用“main”python脚本启动(使用子流程)app1和app2“main”脚本将向app1发送输入,并向app2输出结果,反之亦然(并且主脚本需要记住发送的内容,所以我无法将管道从app1发送到app2)

这是主脚本

import subprocess
import time

def main():
    prvi = subprocess.Popen(['python', 'random1.py'], stdin = subprocess.PIPE , stdout = subprocess.PIPE, stderr = subprocess.STDOUT)

    while 1:
        prvi.stdin.write('131231\n')
        time.sleep(1) # maybe it needs to wait
        print "procitano", prvi.stdout.read()

if __name__ == '__main__':
    main()
这是“random1.py”文件

import random

def main():
    while 1:
        inp = raw_input()
        print inp, random.random()
if __name__ == '__main__':
    main()
首先,我只尝试了一个子流程,只是想看看它是否正常工作。事实并非如此。它只输出“procitano”并在那里等待。 如何读取“prvi”的输出(没有通信()。当我使用它时,它会退出我的应用程序,这是我不想要的)

prvi.stdin.write(…)之后添加
prvi.stdin.flush()

说明:为了优化进程之间的通信,操作系统将在将整个缓冲区发送到另一个进程之前缓冲4KB的数据。如果发送的数据较少,则需要告诉操作系统“就这样了,现在发送”->
flush()

[EDIT]下一个问题是,
prvi.stdout.read()
将永远不会返回,因为子级不会退出

您将需要在进程之间开发一个协议,以便每个进程都知道在获取数据时要读取多少字节的数据。一个简单的解决方案是使用基于行的协议(每条“消息”都由新行终止)。为此,请将
read()
替换为
readline()
,并且不要忘记将
\n
附加到您发送的所有内容+
flush()
prvi.stdin.write(…)
之后添加
prvi.stdin.flush()

说明:为了优化进程之间的通信,操作系统将在将整个缓冲区发送到另一个进程之前缓冲4KB的数据。如果发送的数据较少,则需要告诉操作系统“就这样了,现在发送”->
flush()

[EDIT]下一个问题是,
prvi.stdout.read()
将永远不会返回,因为子级不会退出


您将需要在进程之间开发一个协议,以便每个进程都知道在获取数据时要读取多少字节的数据。一个简单的解决方案是使用基于行的协议(每条“消息”都由新行终止)。为此,请将
read()
替换为
readline()
,并且不要忘记将
\n
附加到您发送的所有内容+
flush()

main.py

import subprocess
import time

def main():
    prvi = subprocess.Popen(['python', 'random1.py'], stdin = subprocess.PIPE , stdout = subprocess.PIPE, stderr = subprocess.STDOUT)


    prvi.stdin.write('131231\n')
    time.sleep(1) # maybe it needs to wait
    print "procitano", prvi.stdout.read()

if __name__ == '__main__':
    main()
import random

def main():

    inp = raw_input()
    print inp, random.random()
    inp = raw_input()

if __name__ == '__main__':
    main()
random1.py

import subprocess
import time

def main():
    prvi = subprocess.Popen(['python', 'random1.py'], stdin = subprocess.PIPE , stdout = subprocess.PIPE, stderr = subprocess.STDOUT)


    prvi.stdin.write('131231\n')
    time.sleep(1) # maybe it needs to wait
    print "procitano", prvi.stdout.read()

if __name__ == '__main__':
    main()
import random

def main():

    inp = raw_input()
    print inp, random.random()
    inp = raw_input()

if __name__ == '__main__':
    main()
我已经用上面的代码进行了测试,然后我遇到了与您的代码相同的问题。 我认为问题在于时机。 这是我的猜测, 当main.py尝试下面的代码时

prvi.stdout.read()  # i think this code may use the random1.py process
下面的代码捕获random1.py进程

inp = raw_input()

要解决这个问题,我认为,正如Aaron Digulla所说,您需要开发协议来实现它。

main.py

import subprocess
import time

def main():
    prvi = subprocess.Popen(['python', 'random1.py'], stdin = subprocess.PIPE , stdout = subprocess.PIPE, stderr = subprocess.STDOUT)


    prvi.stdin.write('131231\n')
    time.sleep(1) # maybe it needs to wait
    print "procitano", prvi.stdout.read()

if __name__ == '__main__':
    main()
import random

def main():

    inp = raw_input()
    print inp, random.random()
    inp = raw_input()

if __name__ == '__main__':
    main()
random1.py

import subprocess
import time

def main():
    prvi = subprocess.Popen(['python', 'random1.py'], stdin = subprocess.PIPE , stdout = subprocess.PIPE, stderr = subprocess.STDOUT)


    prvi.stdin.write('131231\n')
    time.sleep(1) # maybe it needs to wait
    print "procitano", prvi.stdout.read()

if __name__ == '__main__':
    main()
import random

def main():

    inp = raw_input()
    print inp, random.random()
    inp = raw_input()

if __name__ == '__main__':
    main()
我已经用上面的代码进行了测试,然后我遇到了与您的代码相同的问题。 我认为问题在于时机。 这是我的猜测, 当main.py尝试下面的代码时

prvi.stdout.read()  # i think this code may use the random1.py process
下面的代码捕获random1.py进程

inp = raw_input()
要解决这个问题,我认为,正如Aaron Digulla所说,您需要开发协议来实现它。

  • 使用
    -u
    标志使random1.py输出无缓冲
  • 使用p.stdout.readline()而不是.read()
由于.read块,不需要time.sleep。

  • 使用
    -u
    标志使random1.py输出无缓冲
  • 使用p.stdout.readline()而不是.read()

时间。睡眠是不必要的,因为。阅读块。

谢谢您的回答,先生,但我以前试过,现在也试过,看看它是否有效。不幸的是,它不是。因为你没有给我任何信息什么不工作,我不能帮助你。我没有从“random1.py”脚本得到任何输出。和以前一样。你用过
random1.py
写过什么吗?从代码中删除
stderr=subprocess.STDOUT
,并添加
sys.stderr.write()
以从子进程打印到控制台。您可能也需要刷新这些文字。谢谢您的回答,先生,但我以前试过,现在也试过,看看是否有效。不幸的是,它不是。因为你没有给我任何信息什么不工作,我不能帮助你。我没有从“random1.py”脚本得到任何输出。和以前一样。你用过
random1.py
写过什么吗?从代码中删除
stderr=subprocess.STDOUT
,并添加
sys.stderr.write()
以从子进程打印到控制台。您可能还需要刷新这些写入。