I am trying to spawn a django process that lives on after the calling script died. But I need it’s PID.
So I wrote the following code:
def runserver():
print("START PID: " + str(os.getpid()))
pid = os.fork()
if pid == 0:
#cmd = "/usr/bin/env python manage.py runserver 0.0.0.0:2869"
print("IN THE CHILD PID: " + str(os.getpid()))
os.execvp("python", ["", "manage.py", "runserver", "0.0.0.0:2869"])
else:
print("PARENT PID: " + str(os.getpid()))
print("CHILD PID: " + str(pid))
updatepid("runserver", pid)
This gives me the following output:
START PID: 13019
PARENT PID: 13019
CHILD PID: 13020
IN THE CHILD PID: 13020
But now when I check the live processes:
> ps aux | grep python | grep -v grep
sandro 13031 0.4 0.3 296080 23756 pts/2 Sl 22:14 0:01 /home/sandro/.virtualenvs/polling/bin/python2.7 manage.py runserver 0.0.0.0:2869
The pid changed! What on earth is going on???
If you see the other PID then there is definitely a new process. You can easily find the place where the new process spawned. Start in
django.core.management.commands.runserverand you’ll come todjango.utils.autoreload.python_reloader. Whenpython_reloadercalled first time in a process it goes torestart_with_reloaderwhere you can see this:Thereby, with your script you get two processes: one where
runserveris executing and one (spawned) with webserver. Check it: