PyDev 和 Django:如何重新启动开发服务器?
我是姜戈的新手。我想我犯了一个简单的错误。
我使用 Pydev 启动了开发服务器:
R点击项目>>姜戈>>风俗 命令>>运行服务器
服务器启动了,一切都很棒。但现在我正试图阻止它,但不知道如何阻止。我在 PyDev 控制台中停止了该进程,并关闭了 Eclipse,但仍从 http://127.0 提供网页.0.1:8000。
我正常地从命令行启动并退出服务器:
python manage.py runserver
但服务器仍在运行。我在这里做错了什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
默认情况下,runserver 命令以自动重新加载模式运行,该模式在单独的进程中运行。这意味着 PyDev 不知道如何停止它,并且不会在控制台窗口中显示其输出。
如果您改为运行命令
runserver --noreload
,自动重新加载器将被禁用。然后您可以看到控制台输出并正常停止服务器。但是,这意味着对 Python 文件的更改只有在手动重新启动服务器后才会生效。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.运行项目 1. 右键单击项目(不是子文件夹) 2. Run As > Pydev:Django
Terminate 1. 在控制台窗口中单击“terminate”
服务器已关闭
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
我通常从控制台运行它。从 PyDev 运行会增加不必要的混乱,并且在您碰巧使用 PyDev 的 GUI 交互式调试之前不会带来任何好处。
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 3.4.1 起)不再需要任何解决方法:
即:PyDev 将在终止进程操作中正确终止子进程,并且即使在定期重新加载的情况下进行调试时,PyDev 也会将调试器附加到子进程。
旧答案(对于 3.4.1 之前的 PyDev 版本):
不幸的是,这是预期的,因为 PyDev 只会杀死父进程(即:就好像您在任务管理器中杀死父进程而不是 ctrl+C 一样)。
解决方案是编辑 Django 本身,以便子进程轮询父进程以了解它是否仍然存在,如果不是则退出......请参阅:如何让子进程在父进程退出后死亡?供参考。
快速浏览一下后,它似乎与 django/utils/autoreload.py 及其启动方式有关 - 因此,需要启动一个线程来不断查看父进程是否还活着,如果不是,则杀死该进程子进程——我报告了 Django 本身的一个错误:https://code.djangoproject。 com/ticket/16982
注意:作为 PyDev 的解决方法,您可以让 Django 分配一个新的控制台(在 PyDev 之外),同时仍然从 PyDev 运行(因此,在 Django 提供正确的解决方案之前,请使用下面的补丁可用于使 Django 自动重新加载分配一个新的控制台——您可以在其中正确使用 Ctrl+C)。
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).
解决方案:在某些项目文件中创建解释器错误。这将导致服务器崩溃。然后可以正常重新启动服务器。
Solution: create an interpreter error in some project file. This will cause the server to crash. Server can then be restarted as normal.
如果您在 Windows 上使用 CMD 进行操作:使用 CTRL+BREAK 退出服务器。
If you operate on Windows using the CMD: Quit the server with CTRL+BREAK.
您可以通过单击
Ctrl
+Pause
键退出。请注意,暂停键可能称为Break
,在某些笔记本电脑中,它是使用组合Fn
+F12
实现的。希望这可能有所帮助。you can quit by clicking
Ctrl
+Pause
keys. Note that the Pause key might be calledBreak
and in some laptops it is made using the combinationFn
+F12
. Hope this might helps.运行 sudo lsof -i:8000
然后运行kill -9 #PID 应该可以杀死运行该服务器的进程。
然后你可以再次在该端口上使用 python manage.py 服务器
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