我在 shell 脚本上有一个这样写的脚本
function test1(){ command1 };
function test2(){ command2 };
function test3(){ command3 };
我在 shell 脚本上使用 &
来运行那些守护程序编写的函数 test1 & test2 & test3
但我想在 Python 上做同样的事情。有没有办法使用任何 python 内置函数而不使用“daemonize”库?
编辑:我想我应该写得更好。我现在可以看到“背景”是这个问题的更好词。我的意图是加入我阅读的内容here使用 python 命令来制作类似守护进程的东西。 tripleee comment已经帮我回答了。
感谢所有留下评论的人,对于错误,我们深表歉意。
我没有足够的声望,无法给分或添加评论。
最佳答案
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
... 其中 test1
、example2
和 demo3
是您希望与主/父进程同时运行的 Python 函数。
如果您不想并行运行 Python 代码,subprocess.Popen()
会创建一个独立于 Python 程序运行外部命令的进程。
关于python - python中的shell脚本中的 "&"相当于什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47417279/