[Fixed]-PyDev and Django: how to restart dev server?

14👍

By default, the runserver command runs in autoreload mode, which runs in a separate process. This means that PyDev doesn’t know how to stop it, and doesn’t display its output in the console window.

If you run the command runserver --noreload instead, the auto-reloader will be disabled. Then you can see the console output and stop the server normally. However, this means that changes to your Python files won’t be effective until you manually restart the server.

5👍

Run the project 1. Right click on the project (not subfolders) 2. Run As > Pydev:Django

Terminate 1. Click terminate in console window

The server is down

👤Artur

4👍

I usually run it from console. Running from PyDev adds unnecessary confusion, and doesn’t bring any benefit until you happen to use PyDev’s GUI interactive debugging.

👤skrat

3👍

Edit: Latest PyDev versions (since PyDev 3.4.1) no longer need any workaround:

i.e.: PyDev will properly kill subprocesses on a kill process operation and when debugging even with regular reloading on, PyDev will attach the debugger to the child processes.


Old answer (for PyDev versions older than 3.4.1):

Unfortunately, that’s expected, as PyDev will simply kill the parent process (i.e.: as if instead of ctrl+C you kill the parent process in the task manager).

The solution would be editing Django itself so that the child process polls the parent process to know it’s still alive and exit if it’s not… see: How to make child process die after parent exits? for a reference.

After a quick look it seems related to django/utils/autoreload.py and the way it starts up things — so, it’d be needed to start a thread that keeps seeing if the parent is alive and if it’s not it kills the child process — I’ve reported that as a bug in Django itself: https://code.djangoproject.com/ticket/16982

Note: as a workaround for PyDev, you can make Django allocate a new console (out of PyDev) while still running from PyDev (so, until a proper solution is available from Django, the patch below can be used to make the Django autoreload allocate a new console — where you can properly use Ctrl+C).

Index: django/utils/autoreload.py
===================================================================
--- django/utils/autoreload.py  (revision 16923)
+++ django/utils/autoreload.py  (working copy)
@@ -98,11 +98,14 @@
 def restart_with_reloader():
     while True:
         args = [sys.executable] + ['-W%s' % o for o in sys.warnoptions] + sys.argv
-        if sys.platform == "win32":
-            args = ['"%s"' % arg for arg in args]
         new_environ = os.environ.copy()
         new_environ["RUN_MAIN"] = 'true'
-        exit_code = os.spawnve(os.P_WAIT, sys.executable, args, new_environ)
+
+        import subprocess
+        popen = subprocess.Popen(args, env=new_environ, creationflags=subprocess.CREATE_NEW_CONSOLE)
+        exit_code = popen.wait()
         if exit_code != 3:
             return exit_code

2👍

Solution: create an interpreter error in some project file. This will cause the server to crash. Server can then be restarted as normal.

1👍

If you operate on Windows using the CMD: Quit the server with CTRL+BREAK.

python manage.py runserver localhost:8000

0👍

you can quit by clicking Ctrl+ Pause keys. Note that the Pause key might be called Break and in some laptops it is made using the combination Fn + F12. Hope this might helps.

0👍

run sudo lsof -i:8000

then run kill -9 #PID should work to kill the processes running that server.
then you can python manage.py server on that port again

Leave a comment