在完成 main 方法之前终止所有进程
Terminate all processes before finishing the main method
我 运行 一个单独的进程,用于与我的主进程并行的一些日志记录任务。他们共享一些资源,我 运行 遇到在主进程完成之前终止日志进程的问题。
完成主程序 Python 并保留子进程是否有任何缺点?我可以确定它会在退出主程序时终止吗?还是将 Process.terminate()
作为我在主脚本中的最后一次调用会更好?
只要您启动的进程是 daemons
,main process will terminate them automatically before it exits:
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
创建的进程自动设置的,但 defaults to false
for Process
objects。父进程将尝试在所有非守护进程子进程上调用 join
,因此如果您有任何这些 运行,它们将阻止父进程退出,直到它们自己退出。
我 运行 一个单独的进程,用于与我的主进程并行的一些日志记录任务。他们共享一些资源,我 运行 遇到在主进程完成之前终止日志进程的问题。
完成主程序 Python 并保留子进程是否有任何缺点?我可以确定它会在退出主程序时终止吗?还是将 Process.terminate()
作为我在主脚本中的最后一次调用会更好?
只要您启动的进程是 daemons
,main process will terminate them automatically before it exits:
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
创建的进程自动设置的,但 defaults to false
for Process
objects。父进程将尝试在所有非守护进程子进程上调用 join
,因此如果您有任何这些 运行,它们将阻止父进程退出,直到它们自己退出。