如何使用单个shell示例在python中运行多个命令(以避免shell启动时间)

dddzy1tm  于 2021-06-09  发布在  Redis
关注(0)|答案(1)|浏览(421)

这个问题在这里已经有答案了

重新使用和关闭子流程对象的正确方法(4个答案)
9个月前关门了。
我想创建一个 bash (或 zsh 接受命令、运行命令并返回结果的服务器( return code , stdout , stderr ). 我的目的是避免重新开始 bash 每个示例 bash 我想在我的世界里运行的命令 Python 剧本,自从我 bash 加载点文件需要相当长的时间。精神上的东西:

bash = bash_instance() # pay the startup penalty ONCE
bash.run('echo hi') # Just run the command without the startup time
...
bash.run('curl ipinfo.io')
return_code, stdout, stderr = bash.run('cat file.txt')

一种 Remote Method Call 为了贝壳。

eqfvzcg8

eqfvzcg81#

您可以使用多处理:

from multiprocessing import Pool

zsh = zsh_instance()

with Pool(5) as p:
    print(p.map(zsh.run, [
        ('some-command',),
        ('some-other-command',)
    ]))

相关问题