在后台将模块作为子流程启动,并分离 [英] Launch modules as subprocesses in the background, and detach
问题描述
在bash
中,我可以执行以下操作:
for f in subdir/*.sh; do
nohup "$f" "$@" &> /dev/null &
done
换句话说,它在后台运行subdir
中的所有*.sh
脚本,然后将它们分离,以便在主脚本结束时不会终止后台脚本.
现在,假设我有以下Python项目:
proj/
__init__.py
main.py
subdir/
__init__.py
mod_a.py
mod_b.py
mod_c.py
我该如何执行类似于bash脚本的操作?但是将参数作为Python对象传递了吗?
例如:我有两个字符串a
和b
,一个列表l
和一个字典d
- 加载
mod_a.py
,调用mod_a.main(a, b, l, d)
,然后分离 - 加载
mod_b.py
,调用mod_b.main(a, b, l, d)
,然后分离 - 加载
mod_c.py
,调用mod_c.main(a, b, l, d)
,然后分离 -
main.py
可以结束,让mod_a
,mod_b
和mod_c
在后台运行,直到完成
要在Python中模拟nohup
,您可以使子进程忽略SIGHUP
信号:
import signal
def ignore_sighup():
signal.signal(signal.SIGHUP, signal.SIG_IGN)
即模拟bash脚本:
#!/bin/bash
for f in subdir/*.sh; do
nohup "$f" "$@" &> /dev/null &
done
在Python中使用subprocess
模块:
#!/usr/bin/env python3
import sys
from glob import glob
from subprocess import Popen, DEVNULL, STDOUT
for path in glob('subdir/*.sh'):
Popen([path] + sys.argv[1:],
stdout=DEVNULL, stderr=STDOUT, preexec_fn=ignore_sighup)
要创建适当的守护程序,可以使用 python-daemon
程序包.. >
In bash
, I can do the following:
for f in subdir/*.sh; do
nohup "$f" "$@" &> /dev/null &
done
in other words, it runs all *.sh
scripts in subdir
in the background, and detached so that if the main script ends, the background scripts won't be terminated.
Now, let's say I have the following Python project:
proj/
__init__.py
main.py
subdir/
__init__.py
mod_a.py
mod_b.py
mod_c.py
How do I do something similar to the bash script? But with parameters passed as Python objects?
E.g.: I have two strings a
and b
, a list l
, and a dictionary d
- Load
mod_a.py
, invokemod_a.main(a, b, l, d)
, and detach - Load
mod_b.py
, invokemod_b.main(a, b, l, d)
, and detach - Load
mod_c.py
, invokemod_c.main(a, b, l, d)
, and detach main.py
can end, lettingmod_a
,mod_b
, andmod_c
run in the background until completion
To emulate nohup
in Python, you could make child processes to ignore SIGHUP
signal:
import signal
def ignore_sighup():
signal.signal(signal.SIGHUP, signal.SIG_IGN)
i.e., to emulate the bash script:
#!/bin/bash
for f in subdir/*.sh; do
nohup "$f" "$@" &> /dev/null &
done
using subprocess
module in Python:
#!/usr/bin/env python3
import sys
from glob import glob
from subprocess import Popen, DEVNULL, STDOUT
for path in glob('subdir/*.sh'):
Popen([path] + sys.argv[1:],
stdout=DEVNULL, stderr=STDOUT, preexec_fn=ignore_sighup)
To create proper daemons, you could use python-daemon
package.
这篇关于在后台将模块作为子流程启动,并分离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!