保留子进程程序 运行 并接受新参数
Keep the subprocess program running and accepting new arguments
我的程序每次用 Python 的子进程打开时大约需要 4-5 秒才能启动:
command = ['./command', arg1] # my command to launch the program
p = subprocess.Popen(command, stdout=subprocess.PIPE, bufsize=-1) # launch the program (takes about 4 seconds)
print(p.stdout.read()) # display output
我想避免每次都等待程序启动,而是保留它 运行 并传递新的参数。有没有办法保留命令 运行 并将新参数传递给输出?
我希望是这样的:
p = launch_the_program()
p.run(arg1)
p.run(arg2)
p.run(arg3)
p.close()
我知道 this 问题与我的相似,但我认为那里的答案已经过时,因为它对我不起作用。
编辑:链接的答案是关于将 input 传递给程序而不是 arguments (这是我想要实现的).是否有一种解决方法可以保留程序 运行 并在不重新启动的情况下接受新参数?
当 run
returns 时,您 运行 的程序已经退出并且不再在内存中 。 RAM 中没有 pre-initialized 副本可供重复使用(通过 re-invoking 其 main
具有不同的参数)。
我的程序每次用 Python 的子进程打开时大约需要 4-5 秒才能启动:
command = ['./command', arg1] # my command to launch the program
p = subprocess.Popen(command, stdout=subprocess.PIPE, bufsize=-1) # launch the program (takes about 4 seconds)
print(p.stdout.read()) # display output
我想避免每次都等待程序启动,而是保留它 运行 并传递新的参数。有没有办法保留命令 运行 并将新参数传递给输出?
我希望是这样的:
p = launch_the_program()
p.run(arg1)
p.run(arg2)
p.run(arg3)
p.close()
我知道 this 问题与我的相似,但我认为那里的答案已经过时,因为它对我不起作用。
编辑:链接的答案是关于将 input 传递给程序而不是 arguments (这是我想要实现的).是否有一种解决方法可以保留程序 运行 并在不重新启动的情况下接受新参数?
当 run
returns 时,您 运行 的程序已经退出并且不再在内存中 。 RAM 中没有 pre-initialized 副本可供重复使用(通过 re-invoking 其 main
具有不同的参数)。