django 的 runserver 选项是否提供了用于运行其他重启脚本的钩子?
我最近一直在玩 django 和 celery。开发过程中一件烦人的事情是每次修改任务时我都必须重新启动 celery 守护进程。当我开发时,我通常喜欢使用“manage.py runserver”,它会在对我的应用程序进行修改时自动重新加载 django 框架。
有没有办法向 runserver 执行的重新加载过程添加一个钩子,以便它自动重新启动我正在运行的 celery 守护进程?
或者,芹菜是否有类似的监视和更改时重新加载模式,我应该使用它来进行开发?
I've recently been playing around with django and celery. One annoying thing during development is the fact that I have to restart the celery daemon each time I modify a task. When I'm developing, I usually like to use 'manage.py runserver' which automatically reloads the django framework on modifications to my apps.
Is there a way to add a hook to the reloading process that runserver does so that it automatically restarts the celery daemon I have running?
Alternatively, does celery have a similar monitor-and-reload-on-change mode that I should be using for development?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Django-supervisor 非常适合这个目的。您可以让它启动 Django 服务器、Celery 以及您需要的任何其他服务器,并为开发和生产服务器提供不同的配置。它还知道在代码更改时重新加载 celery 守护进程。
https://github.com/rfk/django-supervisor
Django-supervisor works very well for this purpose. You can have it start the Django server, Celery, and anything else you need, and have different configurations for development and production servers. It also knows to reload the celery daemon when your code changes.
https://github.com/rfk/django-supervisor
我相信您可以将
CELERY_ALWAYS_EAGER
设置为true
。I believe you can set
CELERY_ALWAYS_EAGER
totrue
.是的。 Django 提供了自动重新加载钩子,可用于重新启动其他脚本。
这是一个简单的管理命令,它在重新加载时打印一条消息
现在您可以保存到 reload.py 并使用
python manage.py reload
运行它。此处提供了重新加载 celery 工作线程的管理命令。Yes. Django provides auto reload hook, which can be used to restart other scripts.
Here is a simple management command which prints a message on reload
Now you can save to a reload.py and run it with
python manage.py reload
. A management command to reload celery workers is available here.Celery 没有任何重新加载代码或在代码更改时自动重新启动的功能,因此您必须手动重新启动它。
没有办法添加钩子,我认为仅仅为了执行重新启动而编辑 django 源代码是不值得的。
就我个人而言,在开发过程中,我更喜欢看到用颜色装饰的 celery 输出 shell,而不是尾部日志,更具可读性。
Celery didn't have any feature for reload code or for auto restart when the code change, than you have to restart it manually.
There isn't a way for add an hook, and I think not worthwhile of edit the source code of django just for perform a restart.
Personally while I'm developing i prefere to see the output shell of celery that is decorated with color instead of tail the logs, is more readable.
Celery 2.5 有一个实验性的运行时选项 --autoreload 也可以用于此目的。以下是发行说明。话虽这么说,我认为 django-supervisor (来自 @Lee Semel)看起来是更好的做事方式。我想我会在这里发布这个替代方案,以防其他读者不想配置另一个应用程序进行异步处理。
Celery 2.5 has an experimental runtime option --autoreload that could be used for this purpose, too. Here's more detail in the release notes. That being said, I think django-supervisor (via @Lee Semel) looks like the better way of doing things. I thought I would post this alternative here in case other readers do not want to have to configure another app for asynchronous processing.