django 的 runserver 选项是否提供了用于运行其他重启脚本的钩子?

发布于 2024-10-19 12:42:10 字数 254 浏览 1 评论 0原文

我最近一直在玩 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

紫﹏色ふ单纯 2024-10-26 12:42:10

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

口干舌燥 2024-10-26 12:42:10

我相信您可以将 CELERY_ALWAYS_EAGER 设置为 true

I believe you can set CELERY_ALWAYS_EAGER to true.

甜警司 2024-10-26 12:42:10

是的。 Django 提供了自动重新加载钩子,可用于重新启动其他脚本。

这是一个简单的管理命令,它在重新加载时打印一条消息

import subprocess

from django.core.management.base import BaseCommand
from django.utils import autoreload


def reload():
    print('Code changed. Auto reloading...')    

class Command(BaseCommand):

    def handle(self, *args, **options):
        autoreload.main(reload)

现在您可以保存到 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

import subprocess

from django.core.management.base import BaseCommand
from django.utils import autoreload


def reload():
    print('Code changed. Auto reloading...')    

class Command(BaseCommand):

    def handle(self, *args, **options):
        autoreload.main(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.

物价感观 2024-10-26 12:42:10

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.

星光不落少年眉 2024-10-26 12:42:10

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文