0

I'm trying to spawn long processes from a flask application. So I have a main process that handles incoming web requests and from which I start daemons using the python-daemon library. Here is a minimal working example:

import os
import time
import daemon
import lockfile

def daemon_function():
    return True

working_directory = os.path.dirname(os.path.abspath(__file__))
pid_file = os.path.join(working_directory,"my_daemon.pid")

daemon_context = daemon.DaemonContext(
        working_directory=working_directory,
        pidfile=lockfile.FileLock(pid_file),
    )

pid = os.fork()
if pid == 0:
    with daemon_context:
        daemon_function()

time.sleep(10)

When I use the linux command ps -ef while the main process is still running, after the daemon process has terminated, I see this output:

user     2484     1  0 09:38 ?        00:00:01 /lib/systemd/systemd --user
user    11269  6061  0 12:07 pts/2    00:00:00 bash
user    28817 11269  1 15:43 pts/2    00:00:00 python test_daemon.py
user    28818 28817  0 15:43 pts/2    00:00:00 [python] <defunct>

The reason why I use the fork function before the with daemon_context: statement is that I need the main process to continue.

Should I worry about the created defunct process (I might spawn a lot of them) ? How could I avoid this zombie to appear?

4

1 回答 1

1

如果您不想创建僵尸进程,您应该等待它们完成,即执行等待系统调用而不是睡眠:

import os
import time
import daemon
import lockfile

def daemon_function():
    return True

working_directory = os.path.dirname(os.path.abspath(__file__))
pid_file = os.path.join(working_directory,"my_daemon.pid")

daemon_context = daemon.DaemonContext(
        working_directory=working_directory,
        pidfile=lockfile.FileLock(pid_file),
    )

pid = os.fork()
if pid == 0:
    with daemon_context:
        daemon_function()

os.waitpid(pid, 0)
于 2019-03-12T15:05:51.237 回答