PyDev and Django: how to restart dev server?

别等时光非礼了梦想. 提交于 2019-12-28 05:58:10

问题


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?


回答1:


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.




回答2:


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




回答3:


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.




回答4:


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



回答5:


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




回答6:


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

python manage.py runserver localhost:8000



回答7:


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.



来源:https://stackoverflow.com/questions/2746512/pydev-and-django-how-to-restart-dev-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!