Python – 在守护进程中调用multiprocessing.pool

我有一个Python脚本,它产生一个守护进程.在这个过程中,我使用multiprocessing.pool同时运行1到4个进程.当我在守护进程外部运行它时,它运行完美(即,当我设置run_from_debugger = True – 请参阅下面的代码),但如果我...

我有一个Python脚本,它产生一个守护进程.在这个过程中,我使用multiprocessing.pool同时运行1到4个进程.

当我在守护进程外部运行它时,它运行完美(即,当我设置run_from_debugger = True – 请参阅下面的代码),但如果我通过守护进程运行代码(即run_from_debugger = False),则永远不会执行async_function .

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

码:

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()

任何建议将不胜感激.

解决方法:

引自multiprocessing的文档:

daemon

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.

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必须创建工作进程,因此您无法使用它来进行daemonaize进程.

本文标题为:Python – 在守护进程中调用multiprocessing.pool

基础教程推荐