eclipse PyDev 和 Django:如何重新启动开发服务器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2746512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 14:42:02  来源:igfitidea点击:

PyDev and Django: how to restart dev server?

pythondjangoeclipsepydevdevserver

提问by Nick Heiner

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

我是 Django 的新手。我想我犯了一个简单的错误。

I launched the dev server with Pydev:

我用 Pydev 启动了开发服务器:

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

R单击项目>> Django >> 自定义命令>> 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.

服务器上来了,一切都很棒。但现在我试图阻止它,但不知道如何。我在 PyDev 控制台中停止了该进程,并关闭了 Eclipse,但仍然从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?

但是服务器还在。我在这里做错了什么?

回答by interjay

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.

默认情况下,runserver 命令在自动重新加载模式下运行,该模式在单独的进程中运行。这意味着 PyDev 不知道如何停止它,并且不会在控制台窗口中显示其输出。

If you run the command runserver --noreloadinstead, 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.

如果您runserver --noreload改为运行该命令,则自动重新加载器将被禁用。然后就可以看到控制台输出,正常停止服务器了。但是,这意味着在您手动重新启动服务器之前,对 Python 文件的更改不会生效。

回答by Artur

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

运行项目 1. 右键单击​​项目(不是子文件夹) 2. Run As > Pydev:Django

Terminate 1. Click terminate in console window

终止 1. 在控制台窗口中单击终止

The server is down

服务器挂了

回答by skrat

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.

我通常从控制台运行它。从 PyDev 运行会增加不必要的混乱,并且在您碰巧使用 PyDev 的 GUI 交互式调试之前不会带来任何好处。

回答by Fabio Zadrozny

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

编辑:最新的 PyDev 版本(自 PyDev 3.4.1 起)不再需要任何解决方法:

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.

即:PyDev 将在终止进程操作时正确终止子进程,并且即使在定期重新加载的情况下进行调试时,PyDev 也会将调试器附加到子进程。



Old answer (for PyDev versions older than 3.4.1):

旧答案(对于早于 3.4.1 的 PyDev 版本):

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).

不幸的是,这是意料之中的,因为 PyDev 将简单地终止父进程(即:就像你在任务管理器中杀死父进程而不是 ctrl+C 一样)。

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.

解决方案是编辑 Django 本身,以便子进程轮询父进程以了解它是否仍然存在,如果不是,则退出...请参阅:如何在父进程退出后使子进程死亡?供参考。

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

快速查看后,它似乎与 django/utils/autoreload.py 及其启动方式有关 - 因此,需要启动一个线程来不断查看父进程是否还活着,如果不是,它会杀死子进程——我已经将其报告为 Django 本身的一个错误: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).

注意:作为 PyDev 的解决方法,您可以让 Django 分配一个新的控制台(在 PyDev 之外),同时仍然从 PyDev 运行(因此,在 Django 提供适当的解决方案之前,下面的补丁可用于使 Django 自动重新加载分配一个新的控制台——您可以在其中正确使用 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

回答by Nick Heiner

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

解决方案:在某些项目文件中创建解释器错误。这将导致服务器崩溃。然后可以正常重新启动服务器。

回答by ticktack

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

如果您使用 CMD 在 Windows 上操作:使用CTRL+退出服务器BREAK

python manage.py runserver localhost:8000

回答by A_Matar

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

您可以通过单击Ctrl+Pause键退出。请注意,可能会调用 Pause 键,Break并且在某些笔记本电脑中它是使用组合Fn+ 制作的F12。希望这可能会有所帮助。