Python-在守护程序中调用multiprocessing.pool [英] Python - calling multiprocessing.pool inside a daemon

查看:132
本文介绍了Python-在守护程序中调用multiprocessing.pool的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Python脚本,它生成一个守护进程.在进程内部,我正在使用multiprocessing.pool同时运行14进程.

I have a Python script which spawns a daemon process. Inside the process, I am using multiprocessing.pool to run 1 to 4 processes simultaneously.

当我在守护进程之外运行此代码时,它可以正常工作(即,当我设置run_from_debugger=True-参见下面的代码),但是如果我通过守护进程(例如,run_from_debugger=False)运行代码,则async_function从不执行.

When I run this outside the daemon process, it works perfectly (i.e., when I set run_from_debugger=True - see code below), but if I run the code via a daemon process, (i.e., run_from_debugger=False), async_function is never executed.

是否可以在daemon进程内使用multiprocessing.pool ??? 我正在使用Python-daemon 1.6作为守护程序包(如果有的话).

Is it possible to use multiprocessing.pool inside a daemon process??? I am using Python-daemon 1.6 as my daemon package (if it matters).

代码:

def loop_callback(params):
    #Spawn the process in the pool
    #  Because loop_callback is called many times, often faster than async_function executes,
    #  adding them to a pool allows for parallel execution.
    pool.apply_async(async_function, params)


def run_service():
    # loop is a method that can/will call loop_callback multiple times, and it will call
    #   loop_callback faster than the code in asyc_function executes
    loop(alignment_watch_folder, sleep_duration)


#Class declaration
app = App()

#Declare a pool of processes
#  processes=1 indicates serial execution
pool = Pool(processes=4)

#Either run from a daemon process or not
run_from_debugger = False

#Run the daemon process
if run_from_debugger:
    run_service()
else:
    daemon_runner = runner.DaemonRunner(app)
    daemon_runner.do_action()

任何建议将不胜感激.

推荐答案

引用

守护程序

进程的守护程序标志,一个布尔值.必须在调用start()之前进行设置.

The process’s daemon flag, a Boolean value. This must be set before start() is called.

初始值是从创建过程继承的.

The initial value is inherited from the creating process.

进程退出时,它将尝试终止其所有守护进程.

When a process exits, it attempts to terminate all of its daemonic child processes.

请注意,不允许后台进程创建子进程.否则,后台进程将离开其子进程 如果在其父进程退出时终止,则将其孤立. 另外,这些不是Unix守护程序或服务,它们是正常的 非守护进程将终止(而不加入)的进程 进程已退出.

Note that a daemonic process is not allowed to create child processes. Otherwise a daemonic process would leave its children orphaned if it gets terminated when its parent process exits. Additionally, these are not Unix daemons or services, they are normal processes that will be terminated (and not joined) if non-daemonic processes have exited.

由于multiprocessing.Pool必须创建工作进程,因此无法使用它来守护进程.

Since multiprocessing.Pool has to create worker processes, you cannot daemonaize a process using it.

这篇关于Python-在守护程序中调用multiprocessing.pool的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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