与持久化python进程(或多进程)交互

2024-05-28 18:26:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我想这样做:

import MyProcess

proc1 = MyProcess('python').start()
proc2 = MyProcess('bash').start()

### This would mimic what happens in a python shell
print(proc1('a=10')) ==> ___
print(proc1('a*2')) ==> 20
proc1('b=a*2')
print(proc1("print(b)") ==> 20

### This would mimic what happens in a bash shell
proc2("a=hello")
proc2("b=there")
c = proc2("echo \"$a $b\"")
print(c) ==> hello there

proc1.stop()
proc2.stop()

我真的不知道从哪里开始。我尝试使用subprocess,但是只要从我的上一个命令(用process.stdin.write发出)中读回stdout,进程似乎就退出了,并且不会执行任何进一步的命令。此外,多处理可能更好,因为它可以更好地利用资源,如多核。你知道吗

我找不到任何多重处理的例子,比如启动一个进程,发出一个命令,获取输出,发出另一个命令,获取输出,等等。似乎总是关于启动进程,然后让它完成。我需要这个过程来包装某种while True循环吗?你知道吗


Tags: in命令bash进程shellthiswhatstart

热门问题