Question

I need to spawn a background process in django, the view returns immediately, the background process continues make some changes, then update the db. This is done by os.spawnl() function to call a separate .py file.

The problem is after the background process is done, it becames a zombie function [python] <defunct>.

How do I avoid that? I followed this and this example but I still got the child process as zombie after the django render process.

I want to take this chance to practice my *nix process management skills so please do me a favor, don't give me Celery or other mq/async task solutions, and I hate dependencies.

Was it helpful?

Solution

This got to long for a comment-

The wait syscall (which os.wait is a wrapper for) reaps exit codes/pids from dead processes. You will want to os.wait in the process that is a generation above your zombie processes; the parent of the zombies processes. The parent processes will receive a SIGCHLD signal when one of its child processes die. If you insist on doing all of this yourself, you will need to install a signal handler to trap for SIGCHLD and in the signal handler call os.wait. Read some documentation on unix process handling and the Python documentation on the os module as there are variations of the os.wait function that will be non-blocking which maybe helpful.

import signal
signal.signal(signal.SIGCHLD, lambda _x,_y: os.wait())

OTHER TIPS

I had a similar problem. I used active_children() from multiprocessing module.

import multiprocessing

# somewhere in middleware or where appropriate call
active_children()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top