什么是"&"来自python中的shell脚本

时间:2017-11-21 15:55:18

标签: python linux function shell

我在shell脚本上以这种方式编写了一个脚本

function test1(){ command1 };
function test2(){ command2 };
function test3(){ command3 };

我在shell脚本上使用&来运行守护程序test1 & test2 & test3

但我想在Python上做同样的事情。有没有办法使用任何python内置函数并且不使用" daemonize"库中?

编辑:我想我应该写得更好。我现在可以看到"背景"是问题的更好的词。我的目的是加入我用python命令读取here的东西来制作像daemonized一样的东西。 tripleee comment已经为我回答了问题。

感谢所有发表评论并抱歉的人。

由于我没有足够的声誉,我无法给予积分或添加评论。

1 个答案:

答案 0 :(得分:0)

Python multiprocessing module允许您同时运行多个进程,就像shell的后台作业一样。

from multiprocessing import Process

if __name__ == '__main__':
    t1 = Process(target=test1, args=('bob',))
    t1.start()
    t2 = Process(target=example2)
    t2.start()
    t3 = Process(target=demo3, args=('steve jobs', 'bill gates'))
    t3.start()
    # ... Do other stuff

...其中test1example2demo3是您希望与主/父进程同时运行的Python函数。

如果您不想并行运行Python代码,subprocess.Popen()会创建一个独立于Python程序运行外部命令的进程。