1

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.

2
  • 1
    docs.python.org/2/library/os.html#os.wait Commented Nov 7, 2012 at 20:47
  • @tMC thanks, should I os.wait() in child process or grandchild process? Commented Nov 8, 2012 at 0:40

2 Answers 2

2

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())
Sign up to request clarification or add additional context in comments.

1 Comment

The problem is that signal.signal only works in the main thread, and django likes to keep that thread to itself. Otherwise, you could just do signal.signal(signal.SIGCHLD, signal.SIG_IGN) which won't leave the dead processes as zombies.
1

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

import multiprocessing

# somewhere in middleware or where appropriate call
active_children()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.