Django、RabbitMQ和芹菜——为什么在开发中更新了Django代码之后,芹菜会运行旧版本的任务呢?

时间:2022-02-02 20:29:12

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:

我有一个Django应用程序,它偶尔会向芹菜发送一个任务以进行异步执行。我发现,当我在开发中处理代码时,Django开发服务器知道如何自动检测代码何时发生更改,然后重新启动服务器,以便查看更改。然而,我的应用程序的RabbitMQ/芹菜部分并没有注意到开发中的这些变化。如果我修改代码,以后将在芹菜任务中运行,那么芹菜将继续运行旧版本的代码。我能让它接受改变的唯一方法是:

  1. stop the Celery worker
  2. 停止芹菜工人
  3. stop RabbitMQ
  4. 停止RabbitMQ
  5. reset RabbitMQ
  6. 重置RabbitMQ
  7. start RabbitMQ
  8. 开始RabbitMQ
  9. add the user to RabbitMQ that my Django app is configured to use
  10. 将用户添加到我的Django应用程序要使用的RabbitMQ中
  11. set appropriate permissions for this user
  12. 为该用户设置适当的权限
  13. restart the Celery worker
  14. 重新启动芹菜工人

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

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

1 个解决方案

#1


12  

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.

我发现,当我在开发中处理代码时,Django开发服务器知道如何自动检测代码何时发生更改,然后重新启动服务器,以便查看更改。然而,我的应用程序的RabbitMQ/芹菜部分并没有注意到开发中的这些变化。

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. 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.

问题是,“为什么芹菜不能获得新版本”,但这是大多数库的工作方式。然而,Django开发服务器是一个例外。它有特殊的代码,可以帮助它根据需要自动重新加载Python代码。它基本上重新启动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. 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.

最后,不需要重新启动RabbitMQ。您只需重新启动芹菜工作程序,就可以使用Python代码的新版本。但是,如果新版本的代码正在更改消息中的数据,则可能需要清除队列。例如,在期望收到版本2时,芹菜工人可能正在收到消息的版本1。

#1


12  

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.

我发现,当我在开发中处理代码时,Django开发服务器知道如何自动检测代码何时发生更改,然后重新启动服务器,以便查看更改。然而,我的应用程序的RabbitMQ/芹菜部分并没有注意到开发中的这些变化。

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. 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.

问题是,“为什么芹菜不能获得新版本”,但这是大多数库的工作方式。然而,Django开发服务器是一个例外。它有特殊的代码,可以帮助它根据需要自动重新加载Python代码。它基本上重新启动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. 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.

最后,不需要重新启动RabbitMQ。您只需重新启动芹菜工作程序,就可以使用Python代码的新版本。但是,如果新版本的代码正在更改消息中的数据,则可能需要清除队列。例如,在期望收到版本2时,芹菜工人可能正在收到消息的版本1。