【问题标题】:Is the process created with python `mutliprocessing.Process().daemon=True`, a real daemon process?使用 python `multiprocessing.Process().daemon=True` 创建的进程是真正的守护进程吗?
【发布时间】:2018-09-05 07:14:50
【问题描述】:

我编写了一个服务器,它接受某个端口上的请求并派生一个新的守护进程来处理每个请求。我正在与multiprocessing module 进行分叉。基本代码(没有端口监听逻辑,我不怀疑)如下所示:

代码(mu_min.py)

import time
import multiprocessing as mup
import sys
import os

def worker(name):
    a = 0

    while(a < 5):
        print(name,":",a)
        a = a+1
        time.sleep(2)
        pass 

    print("Exiting worker: ", name, "(", os.getpid(), ")")

def start_server():
    b = 0

    while(b<3):
        new_worker = mup.Process(target=worker,args=('worker-'+str(b),))
        new_worker.daemon = True
        new_worker.start()
        b = b + 1
        time.sleep(3)

    time.sleep(3600)

start_server()

输出

worker-0 : 0
worker-0 : 1
worker-0 : 2
worker-0 : 3
worker-0 : 4
Exiting worker:  worker-0 ( 32831 )
worker-1 : 0
worker-1 : 1
worker-1 : 2
worker-1 : 3
worker-1 : 4
Exiting worker:  worker-1 ( 32834 )
worker-2 : 0
worker-2 : 1
worker-2 : 2
worker-2 : 3
worker-2 : 4
Exiting worker:  worker-2 ( 32837 )

ps 命令输出

如果我在其他终端连续运行ps 命令,我会得到以下输出:

[user@machine mu_min]$ ps axo pid,ppid,pgid,sess,comm --forest | grep -E 'python| PID'
   PID   PPID   PGID   SESS COMMAND
[user@machine mu_min]$ python3 mu_min.py > mu_min_nix_out &
[1] 32830
[user@machine mu_min]$ ps axo pid,ppid,pgid,sess,comm --forest | grep -E 'python| PID'
   PID   PPID   PGID   SESS COMMAND
 32830  25898  32830  25898  |           \_ python3
 32831  32830  32830  25898  |           |   \_ python3
[user@machine mu_min]$ ps axo pid,ppid,pgid,sess,comm --forest | grep -E 'python| PID'
   PID   PPID   PGID   SESS COMMAND
 32830  25898  32830  25898  |           \_ python3
 32831  32830  32830  25898  |           |   \_ python3
 32834  32830  32830  25898  |           |   \_ python3
[user@machine mu_min]$ ps axo pid,ppid,pgid,sess,comm --forest | grep -E 'python| PID'
   PID   PPID   PGID   SESS COMMAND
 32830  25898  32830  25898  |           \_ python3
 32831  32830  32830  25898  |           |   \_ python3
 32834  32830  32830  25898  |           |   \_ python3
 32837  32830  32830  25898  |           |   \_ python3
[user@machine mu_min]$ ps axo pid,ppid,pgid,sess,comm --forest | grep -E 'python| PID'
   PID   PPID   PGID   SESS COMMAND
 32830  25898  32830  25898  |           \_ python3
 32831  32830  32830  25898  |           |   \_ python3 <defunct>
 32834  32830  32830  25898  |           |   \_ python3
 32837  32830  32830  25898  |           |   \_ python3
[user@machine mu_min]$ ps axo pid,ppid,pgid,sess,comm --forest | grep -E 'python| PID'
   PID   PPID   PGID   SESS COMMAND
 32830  25898  32830  25898  |           \_ python3
 32831  32830  32830  25898  |           |   \_ python3 <defunct>
 32834  32830  32830  25898  |           |   \_ python3 <defunct>
 32837  32830  32830  25898  |           |   \_ python3
[user@machine mu_min]$ ps axo pid,ppid,pgid,sess,comm --forest | grep -E 'python| PID'
   PID   PPID   PGID   SESS COMMAND
 32830  25898  32830  25898  |           \_ python3
 32831  32830  32830  25898  |           |   \_ python3 <defunct>
 32834  32830  32830  25898  |           |   \_ python3 <defunct>
 32837  32830  32830  25898  |           |   \_ python3 <defunct>

我有以下疑问

  1. multiprocessing doc page 说如下:

    此外,这些不是 Unix 守护进程或服务,它们是正常进程,如果非守护进程退出,它们将被终止(而不是加入)。

    那么多处理不是创建真正的守护进程吗(如下面的疑问所述)?我也没有理解“如果非守护进程退出,那将被终止(而不是加入)”的含义。什么意思?

  2. 我使用os.fork() 处理signal.SIGCHLD 准备了类似的带有双分叉的代码。处理signal.SIGCHLD 似乎不会留下失效的进程。此外,似乎由于双重分叉,任何进程 created 是用PPID=1 创建的,使它们成为适当的守护进程。请注意,在上面的ps 命令输出PPID 的无效进程不是1。所以它们似乎不是正确的守护进程。是这样吗?

【问题讨论】:

    标签: python python-3.x multithreading multiprocessing


    【解决方案1】:

    回答你的第一个问题:

    这些进程不是真正的 Unix 守护进程,它们是“守护进程”,与进程中的守护线程线程相同。

    如果您的主进程完成并且辅助非守护进程仍在运行,则主进程将“加入”它,即在退出之前等待它完成。但是,如果辅助进程是守护进程,则它将在主进程完成时终止。

    【讨论】:

    • 这能回答我的疑惑吗?我已经非常明确地指出了这一点。
    • 这回答了你的第一个问题。
    • 所以这些守护进程只是父进程不等待完成的子线程......?这是“守护进程”的定义吗?
    • 它们是实际的进程,而不是线程,但它们的行为是这样的。
    • 但是对于所谓的守护进程来说,这是期望的行为吗?它是否遵循standard daemon creation idiomsdouble forking for daemon creation
    猜你喜欢
    • 2012-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-12
    相关资源
    最近更新 更多