保持子流程程序运行并接受新参数



我有一个程序,每次用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

我希望避免每次都等待程序启动,而是让它继续运行并传递新的args。有没有一种方法可以保持命令的运行并将新的参数传递给输出?

我希望有这样的东西:

p = launch_the_program()
p.run(arg1)
p.run(arg2)
p.run(arg3)
p.close()

我知道这个问题和我的相似,但我认为答案已经过时了,因为它对我不起作用

编辑:链接的答案是将输入传递到程序,而不是参数(这正是我试图实现的(。有没有一种变通方法可以让程序在不重新启动的情况下继续运行并接受新的参数?

run返回时,您运行的程序已退出并不再在内存中。RAM中没有可重用的预初始化副本(通过使用不同的参数重新调用其main(。

最新更新