Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does manage.py change its PID?

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???

like image 605
Sandro Avatar asked Mar 11 '12 04:03

Sandro


People also ask

What happens when we run Python manage py Runserver?

This executes the command and perform base checks if needed. This performs normal checks and migration checks if needed and then calls a function called handle . This function makes all the checks regarding IP address like checking port, IP address format ipv4 or ipv6.

What does manage py do in Python?

manage.py is a thin wrapper around django-admin.py that takes care of two things for you before delegating to django-admin.py: It puts your project's package on sys. path. It sets the DJANGO_SETTINGS_MODULE environment variable so that it points to your project's settings.py file.

What is the work of manage py in django?

In addition, manage.py is automatically created in each Django project. It does the same thing as django-admin but also sets the DJANGO_SETTINGS_MODULE environment variable so that it points to your project's settings.py file.

Where should manage py be?

The django-admin.py script should be on your system path if you installed Django via its setup.py utility. If it's not on your path, you can find it in site-packages/django/bin within your Python installation.


1 Answers

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.runserver and you'll come to django.utils.autoreload.python_reloader. When python_reloader called first time in a process it goes to restart_with_reloader where you can see this:

exit_code = os.spawnve(os.P_WAIT, sys.executable, args, new_environ)

Thereby, with your script you get two processes: one where runserver is executing and one (spawned) with webserver. Check it:

START PID: 3091
PARENT PID: 3091
CHILD PID: 3092
IN THE CHILD PID: 3092

$ ps ax | grep runserver | grep -v grep
3092 pts/1    S      0:00  runserver 0.0.0.0:2869
3093 pts/1    Sl     0:05 /home/kirill/testenv/bin/python manage.py runserver 0.0.0.0:2869
like image 195
Kirill Avatar answered Sep 20 '22 16:09

Kirill