python - python中的shell脚本中的 "&"相当于什么

标签 python linux function shell

我在 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

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

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

关于python - python中的shell脚本中的 "&"相当于什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47417279/

相关文章:

python - 如何在 Pandas 数据框中获取列表的长度

python - 通过请求模块调用 View 不会创建正确的 session 数据

python - 变换预测目标

c - 使用 Stat 和 OpenDir()/ReadDir() 在 C 语言中列出当前目录和文件类型(Dir、Reg、Lnk)

linux - 如何通过预置文件安装 rsyslog-pgsql wile install linux with default answers

python - Geopy 在 ASCII 字符上阻塞

java - Linux 从 Java 控制台应用程序中杀死 Java 进程

javascript - 如何访问symfony2中另一个html.twig文件中的javascript函数?

function - 对于良好的日常习惯来说,哪一个特征最重要?

c++ - 是 int & foo();一个左值?