Django,RabbitMQ,&芹菜 - 为什么在开发Django代码之后,Celery会运行旧版本的任务? [英] Django, RabbitMQ, & Celery - why does Celery run old versions of my tasks after I update my Django code in development?

查看:157
本文介绍了Django,RabbitMQ,&芹菜 - 为什么在开发Django代码之后,Celery会运行旧版本的任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个Django应用程序偶尔会向Celery发送一个任务来进行异步执行。我发现,当我开发我的代码时,Django开发服务器知道如何自动检测代码是否已更改,然后重新启动服务器,以便我可以看到我的更改。然而,我的应用程序的RabbitMQ / Celery部分并没有接受这些发展变化。如果我更改以后在Celery任务中运行的代码,Celery仍然会继续运行旧版本的代码。唯一的办法就是取得这个变化:


  1. 停止芹菜工作者

  2. 停止RabbitMQ

  3. 重置RabbitMQ

  4. 启动RabbitMQ

  5. 将用户添加到RabbitMQ,我的Django app被配置为使用

  6. 为此用户设置适当的权限

  7. 重新启动Celery工作者

然而,这似乎比我应该采取的更为激烈的做法。有没有一个更轻量级的方法我可以使用?

解决方案


我发现,在我的代码开发中,Django
开发服务器知道如何自动检测代码是否已更改
,然后重新启动服务器,以便我可以看到我的更改。然而,
我的应用程序的RabbitMQ / Celery部分不会在这些类别上获取
的开发更改。


这里描述的是完全正确和预期的。请记住,Python将使用模块缓存,因此您需要重新启动Python解释器,然后才能使用新的代码。



问题是为什么Celery不接受新版本,但这是大多数图书馆工作。然而,Django开发服务器是一个例外。它有特殊的代码,可以帮助它自动重新加载Python代码。 它基本上重新启动Web服务器,而无需重新启动Web服务器



请注意,当您在生产中运行Django时,您可能必须重新启动/重新加载服务器(因为您不会在生产中使用开发服务器,大多数生产服务器不会试图实施检测文件更改和自动重新加载服务器的问题特征的麻烦)。



最后,需要重新启动RabbitMQ。您只需要重新启动Celery工作者才能使用新版本的Python代码。但是,如果新版本的代码正在更改消息中的数据,则可能需要清除队列。例如,当期望收到版本2时,Celery工作人员可能会收到该消息的第1版。


So I have a Django app that occasionally sends a task to Celery for asynchronous execution. I've found that as I work on my code in development, the Django development server knows how to automatically detect when code has changed and then restart the server so I can see my changes. However, the RabbitMQ/Celery section of my app doesn't pick up on these sorts of changes in development. If I change code that will later be run in a Celery task, Celery will still keep running the old version of the code. The only way I can get it to pick up on the change is to:

  1. stop the Celery worker
  2. stop RabbitMQ
  3. reset RabbitMQ
  4. start RabbitMQ
  5. add the user to RabbitMQ that my Django app is configured to use
  6. set appropriate permissions for this user
  7. restart the Celery worker

This seems like a far more drastic approach than I should have to take, however. Is there a more lightweight approach I can use?

解决方案

I've found that as I work on my code in development, the Django development server knows how to automatically detect when code has changed and then restart the server so I can see my changes. However, the RabbitMQ/Celery section of my app doesn't pick up on these sorts of changes in development.

What you've described here is exactly correct and expected. Keep in mind that Python will use a module cache, so you WILL need to restart the Python interpreter before you can use the new code.

The question is "Why doesn't Celery pick up the new version", but this is how most libraries will work. The Django development server, however, is an exception. It has special code that helps it automatically reload Python code as necessary. It basically restarts the web server without you needing to restart the web server.

Note that when you run Django in production, you probably WILL have to restart/reload your server (since you won't be using the development server in production, and most production servers don't try to take on the hassle of implementing a problematic feature of detecting file changes and auto-reloading the server).

Finally, you shouldn't need to restart RabbitMQ. You should only have to restart the Celery worker to use the new version of the Python code. You might have to clear the queue if the new version of the code is changing the data in the message, however. For example, the Celery worker might be receiving version 1 of the message when it is expecting to receive version 2.

这篇关于Django,RabbitMQ,&芹菜 - 为什么在开发Django代码之后,Celery会运行旧版本的任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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