17

I'm new to Django. I think I'm making a simple mistake.

I launched the dev server with Pydev:

RClick on project >> Django >> Custom command >> runserver

The server came up, and everything was great. But now I'm trying to stop it, and can't figure out how. I stopped the process in the PyDev console, and closed Eclipse, but web pages are still being served from http://127.0.0.1:8000.

I launched and quit the server from the command line normally:

python manage.py runserver

But the server is still up. What am I doing wrong here?

skaffman
  • 398,947
  • 96
  • 818
  • 769
Nick Heiner
  • 119,074
  • 188
  • 476
  • 699

8 Answers8

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.

interjay
  • 107,303
  • 21
  • 270
  • 254
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
  • 268
  • 4
  • 8
  • This works, though "Terminate" is the red box to the right, and then you have to restart the server in order to see any changes. – Marcel Marino Apr 14 '13 at 19:10
  • Great solution, and if it doesn't work then try restarting the computer/eclipse and making sure you only start the server this exact way. Mixing this with "runserver" custom commands can prevent the server from being stopped properly. – ecoe Mar 06 '14 at 01:33
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
  • 5,518
  • 3
  • 32
  • 48
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
Community
  • 1
  • 1
Fabio Zadrozny
  • 24,814
  • 4
  • 66
  • 78
2

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

Nick Heiner
  • 119,074
  • 188
  • 476
  • 699
1

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

python manage.py runserver localhost:8000
rolve
  • 10,083
  • 4
  • 55
  • 75
ticktack
  • 21
  • 1
  • 4
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.

A_Matar
  • 2,210
  • 3
  • 31
  • 53
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

halfelf
  • 9,737
  • 13
  • 54
  • 63