我有一个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


基础教程推荐
- python matplotlib各种画图 2023-08-04
- 关于pycharm python3.7成功安装dlib库的问题 2023-08-04
- 如何在python进程中限制内存使用 2023-11-16
- Python实现免费音乐下载器 2023-08-04
- 进程与线程(3)- python实现多线程 2023-09-04
- 在OS X上运行的Python中读/写另一个进程内存 2023-11-14
- python-在Ubuntu 14.04上构建Pylucene(可信任的Tahr) 2023-11-12
- python-将代码包装在函数中是否可以获得内存效率? 2023-11-10
- python图形界面tkinter的使用技巧 2022-10-20
- linux下升级python版本 2023-11-11