本文介绍了芹菜自动重新加载任何更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当 settings.py 中的 CELERY_IMPORTS 中的模块发生更改时,我可以使芹菜自动重新加载。 / p>

我试图让母模块甚至在子模块上也可以检测到更改,但是它没有检测到子模块中的更改。这使我明白芹菜不会递归地进行检测。我在文档中进行了搜索,但是没有收到任何针对我问题的答复。



将项目中与芹菜相关的所有内容都添加到<$ c中,确实让我感到困扰。 $ c> CELERY_IMPORTS 来检测更改。



有没有一种方法可以告诉芹菜当项目中的任何地方发生变化时自动重新加载自己的内容。



谢谢!

解决方案

您可以使用 -I | --include手动添加其他模块。将其与 find 和 awk 之类的GNU工具结合使用,您将能够找到所有 .py 文件并包含它们。

  $ celery-应用工作者--autoreload --include = $(查找。-name * .py -type f | awk'{sub( \。/,); gsub( /,。); sub(。py, );打印}'ORS =','| sed's /.$//')

让我们解释一下:

  find。 -name * .py -type f 

查找递归搜索包含 .py 的所有文件。输出看起来像这样:

  ./ app.py 
./some_package/foopy
。 /some_package/bar.py

然后:

  awk'{sub( \。/,); gsub( /,。); sub(。py,);打印}'ORS =','

此行的输出为 find 作为输入,并删除所有出现的 ./ 。然后,将所有 / 替换为。。最后一个 sub()删除,将 .py 替换为空字符串。 ORS 用,替换所有换行符。输出结果:

  app,some_package.foo,some_package.bar,

最后一个命令 sed 删除最后一个



因此正在执行的命令如下:

  $ celery-应用程序工作人员--autoreload --include = app,some_package.foo,some_package.bar 

如果您的源文件中有 virtualenv ,则可以通过添加 -path .path_to_your_env -prune -o 来排除它:

  $ celery-应用工作者--autoreload --include = $(find。-path .path_to_your_env -prune -o -name  * .py -type f | awk'{sub( \。/,); gsub( /,。); sub(。py,);打印}' ORS =','| sed's /.$//')


I could make celery reload itself automatically when there is changes on modules in CELERY_IMPORTS in settings.py.

I tried to give mother modules to detect changes even on child modules but it did not detect changes in child modules. That make me understand that detecting is not done recursively by celery. I searched it in the documentation but I did not meet any response for my problem.

It is really bothering me to add everything related celery part of my project to CELERY_IMPORTS to detect changes.

Is there a way to tell celery that "auto reload yourself when there is any changes in anywhere of project".

Thank You!

解决方案

You can manually include additional modules with -I|--include. Combine this with GNU tools like find and awk and you'll be able to find all .py files and include them.

$ celery -A app worker --autoreload --include=$(find . -name "*.py" -type f | awk '{sub("\./",""); gsub("/", "."); sub(".py",""); print}' ORS=',' | sed 's/.$//')

Lets explain it:

find . -name "*.py" -type f

find searches recursively for all files containing .py. The output looks something like this:

./app.py
./some_package/foopy
./some_package/bar.py

Then:

awk '{sub("\./",""); gsub("/", "."); sub(".py",""); print}' ORS=','

This line takes output of find as input and removes all occurences of ./. Then it replaces all / with a .. The last sub() removes replaces .py with an empty string. ORS replaces all newlines with ,. This outputs:

app,some_package.foo,some_package.bar,

The last command, sed removes the last ,.

So the command that is being executed looks like:

$ celery -A app worker --autoreload --include=app,some_package.foo,some_package.bar

If you have a virtualenv inside your source you can exclude it by adding -path .path_to_your_env -prune -o:

$ celery -A app worker --autoreload --include=$(find . -path .path_to_your_env -prune -o -name "*.py" -type f | awk '{sub("\./",""); gsub("/", "."); sub(".py",""); print}' ORS=',' | sed 's/.$//')

这篇关于芹菜自动重新加载任何更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-29 09:37