在后台将模块作为子流程启动,并分离 [英] Launch modules as subprocesses in the background, and detach

查看:114
本文介绍了在后台将模块作为子流程启动,并分离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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对象传递了吗?

例如:我有两个字符串ab,一个列表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_amod_bmod_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, invoke mod_a.main(a, b, l, d), and detach
  • Load mod_b.py, invoke mod_b.main(a, b, l, d), and detach
  • Load mod_c.py, invoke mod_c.main(a, b, l, d), and detach
  • main.py can end, letting mod_a, mod_b, and mod_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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆