简体   繁体   English

Django,RabbitMQ和Celery - 为什么Celery在开发中更新我的Django代码后会运行旧版本的任务?

[英]Django, RabbitMQ, & Celery - why does Celery run old versions of my tasks after I update my Django code in development?

So I have a Django app that occasionally sends a task to Celery for asynchronous execution. 所以我有一个Django应用程序偶尔会向Celery发送任务以进行异步执行。 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. 我发现,当我在开发中处理我的代码时,Django开发服务器知道如何自动检测代码何时更改,然后重新启动服务器以便我可以看到我的更改。 However, the RabbitMQ/Celery section of my app doesn't pick up on these sorts of changes in development. 但是,我的应用程序的RabbitMQ / Celery部分没有了解开发中的这些变化。 If I change code that will later be run in a Celery task, Celery will still keep running the old version of the code. 如果我更改稍后将在Celery任务中运行的代码,Celery仍将继续运行旧版本的代码。 The only way I can get it to pick up on the change is to: 我能够接受改变的唯一方法是:

  1. stop the Celery worker 停止芹菜工人
  2. stop RabbitMQ 停止RabbitMQ
  3. reset RabbitMQ 重置RabbitMQ
  4. start RabbitMQ 启动RabbitMQ
  5. add the user to RabbitMQ that my Django app is configured to use 将用户添加到我的Django应用程序配置使用的RabbitMQ
  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. 我发现,当我在开发中处理我的代码时,Django开发服务器知道如何自动检测代码何时更改,然后重新启动服务器以便我可以看到我的更改。 However, the RabbitMQ/Celery section of my app doesn't pick up on these sorts of changes in development. 但是,我的应用程序的RabbitMQ / Celery部分没有了解开发中的这些变化。

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. 请记住,Python将使用模块缓存 ,因此您需要在使用新代码之前重新启动Python解释器。

The question is "Why doesn't Celery pick up the new version", but this is how most libraries will work. 问题是“为什么Celery没有拿到新版本”,但这就是大多数图书馆的工作方式。 The Django development server, however, is an exception. 但是,Django开发服务器是个例外。 It has special code that helps it automatically reload Python code as necessary. 它有特殊的代码,可以帮助它根据需要自动重新加载Python代码。 It basically restarts the web server without you needing to restart the web server . 它基本上重新启动Web服务器,而无需重新启动Web服务器

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). 请注意,当您在生产中运行Django时,您可能必须重新启动/重新加载您的服务器(因为您不会在生产中使用开发服务器,并且大多数生产服务器不会尝试解决实施问题的麻烦检测文件更改和自动重新加载服务器的功能)。

Finally, you shouldn't need to restart RabbitMQ. 最后,您不需要重新启动RabbitMQ。 You should only have to restart the Celery worker to use the new version of the Python code. 您只需重新启动Celery工作程序即可使用新版本的Python代码。 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. 例如,当Celery工作程序期望接收版本2时,它可能正在接收消息的版本1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM