Django和Celery-更改后将代码重新加载到Celery中 [英] Django and Celery - re-loading code into Celery after a change

查看:548
本文介绍了Django和Celery-更改后将代码重新加载到Celery中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在celery运行时对task.py进行了更改,是否有一种机制可以重新加载更新的代码?还是我必须关闭Celery重新加载?

If I make a change to tasks.py while celery is running, is there a mechanism by which it can re-load the updated code? or do I have to shut Celery down a re-load?

我读到celery有-autoreload 参数在旧版本中,但在当前版本中找不到:

I read celery had an --autoreload argument in older versions, but I can't find it in the current version:

celery:错误:无法识别的参数:--autoreload

推荐答案

不幸的是,-自动重新加载不起作用并且它已已弃用

Unfortunately --autoreload doesn't work and it is deprecated.

您可以使用Watchdog,它为watchmedo提供了shell实用程序,可以根据文件事件执行操作。

You can use Watchdog which provides watchmedo a shell utilitiy to perform actions based on file events.

pip install watchdog

您可以使用

watchmedo auto-restart -- celery worker -l info -A foo

默认情况下,它将监视当前目录中的所有文件。可以通过传递相应的参数来更改这些参数。

By default it will watch for all files in current directory. These can be changed by passing corresponding parameters.

watchmedo auto-restart -d . -p '*.py' -- celery worker -l info -A foo

添加 -R 选项以递归方式查看文件。

Add -R option to recursively watch the files.

如果您使用的是django,并且不想依赖看门狗,则有一个简单的技巧可以实现此目的。 Django具有autoreload实用程序,当代码更改时,runserver会使用它来重新启动WSGI服务器。

If you are using django and don't want to depend on watchdog, there is a simple trick to achieve this. Django has autoreload utility which is used by runserver to restart WSGI server when code changes.

相同的功能可用于重新加载celery worker。创建一个单独的管理命令,称为celery。编写一个函数来杀死现有的工作人员并开始新的工作人员。现在,按如下所示将该函数挂钩以自动重新加载。对于Django> = 2.2

The same functionality can be used to reload celery workers. Create a seperate management command called celery. Write a function to kill existing worker and start a new worker. Now hook this function to autoreload as follows. For Django >= 2.2

import sys

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


class Command(BaseCommand):
    def handle(self, *args, **options):
        autoreload.run_with_reloader(self._restart_celery)

    @classmethod
    def _restart_celery(cls):
        if sys.platform == "win32":
            cls.run('taskkill /f /t /im celery.exe')
            cls.run('celery -A phoenix worker --loglevel=info --pool=solo')
        else:  # probably ok for linux2, cygwin and darwin. Not sure about os2, os2emx, riscos and atheos
            cls.run('pkill celery')
            cls.run('celery worker -l info -A foo')

    @staticmethod
    def run(cmd):
        subprocess.call(shlex.split(cmd))

对于Django< 2.2

For django < 2.2

import sys

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


class Command(BaseCommand):
    def handle(self, *args, **options):
        autoreload.main(self._restart_celery)

    @classmethod
    def _restart_celery(cls):
        if sys.platform == "win32":
            cls.run('taskkill /f /t /im celery.exe')
            cls.run('celery -A phoenix worker --loglevel=info --pool=solo')
        else:  # probably ok for linux2, cygwin and darwin. Not sure about os2, os2emx, riscos and atheos
            cls.run('pkill celery')
            cls.run('celery worker -l info -A foo')

    @staticmethod
    def run(cmd):
        subprocess.call(shlex.split(cmd))

现在您可以使用 python manage.py celery 运行celery worker,它将在代码库更改时自动重新加载。

Now you can run celery worker with python manage.py celery which will autoreload when codebase changes.

这仅用于开发目的,请勿在生产中使用。

This is only for development purposes and do not use it in production.

这篇关于Django和Celery-更改后将代码重新加载到Celery中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆