Real Python · Martin Breuss
Asynchronous Tasks With Django and Celery
能在一个 Django 项目中接入 Celery 与 Redis,把耗时任务改为异步执行并验证结果。
Python
- 难度
- 见前置条件
- 形式
- 实践教程
- 语言
- en
- 规模
- 15 个课节
- 内容费用
- 免费
- 动手
- 需要动手环境
使用限制
学习正文可直接在网页阅读,无需登录;跟做练习需自备本地环境与 Redis。
- 练习与作业
- 示例代码下载包(原文说可免费下载)
选择学习环境
按你的电脑系统准备;提示词在你自己的电脑上使用。
本机动手
这是 Real Python 的实践教程:以一个反馈表单应用为例,把同步发送邮件改为 Celery 异步任务。文章的示例在现有 Django 应用上进行,Celery 自 4.x 起不支持 Windows。
需要能运行 Python 与 Django 的环境;文章聚焦 UNIX 系统上的 Celery,并说明 Celery 自 4.x 起不再支持 Windows。本地还需安装 Redis。
准备工具和账号
标“选用”的可以跳过。
让 AI Agent 一次搭好课程
复制开课提示词发给你常用的 AI Agent。它会下载允许保存的官方材料、建立课节目录并自检;需要账号、权限或费用时会先问你。
开课提示词 · 只需用一次此提示词尚未实测验证请帮我把「Asynchronous Tasks With Django and Celery」的学习环境一次准备好,让我之后只需要按顺序学习和做练习。 开始前先问我:课程放在哪个目录(等我给出完整路径),确认我的操作系统和已有工具。确认前不要创建文件或安装软件。 课程入口:https://realpython.com/asynchronous-tasks-with-django-and-celery/ 我的系统:macOS。如果实际不符,先调整方案。 本次路线:macOS:本机动手。只准备这条路线需要的工具;只读路线不安装运行环境、不配置模型密钥。 练习:把示例项目中的发送邮件函数改造成 Celery 任务,对比改造前后的响应时间;文章还提示可以用 RabbitMQ 替换 Redis 作为 broker 作为挑战。 课节(官方顺序): 1. Asynchronous Tasks With Django and Celery(教程主页):https://realpython.com/asynchronous-tasks-with-django-and-celery/ 2. Python Celery Basics:https://realpython.com/asynchronous-tasks-with-django-and-celery/ 3. Why Use Celery?:https://realpython.com/asynchronous-tasks-with-django-and-celery/ 4. How Can You Leverage Celery for Your Django App?:https://realpython.com/asynchronous-tasks-with-django-and-celery/ 5. Integrate Celery With Django:https://realpython.com/asynchronous-tasks-with-django-and-celery/ 6. Set Up the Feedback App:https://realpython.com/asynchronous-tasks-with-django-and-celery/ 7. Install Celery as Your Task Queue:https://realpython.com/asynchronous-tasks-with-django-and-celery/ 8. Install Redis as Your Celery Broker and Database Back End:https://realpython.com/asynchronous-tasks-with-django-and-celery/ 9. Add Celery to Your Django Project:https://realpython.com/asynchronous-tasks-with-django-and-celery/ 10. Handle Workloads Asynchronously With Celery:https://realpython.com/asynchronous-tasks-with-django-and-celery/ 11. Revisit the Synchronous Code:https://realpython.com/asynchronous-tasks-with-django-and-celery/ 12. Refactor the Code as a Celery Task:https://realpython.com/asynchronous-tasks-with-django-and-celery/ 13. Test Your Asynchronous Task:https://realpython.com/asynchronous-tasks-with-django-and-celery/ 14. Conclusion:https://realpython.com/asynchronous-tasks-with-django-and-celery/ 15. Frequently Asked Questions:https://realpython.com/asynchronous-tasks-with-django-and-celery/ 1. 环境 - Python 3 环境 - Django 项目(使用文章提供的示例代码) - 本地 Redis 服务 - Celery - 安装前先列出要装的软件和命令,等我同意;已装好的直接复用。 2. 材料 - 官方课程页面:只记录链接 https://realpython.com/asynchronous-tasks-with-django-and-celery/ 3. 目录(最外层放在我确认的位置) django-celery-async-tasks/ ├── README.md # 课程入口、课节清单、每次怎么开始 ├── progress.md # 每课:读讲义 / 做练习 / 记笔记 ├── questions.md # 待解决的问题 ├── check-env.sh # 环境自检,按 README 操作 ├── data/ # 练习用到的样例数据 └── lessons/ # 每课一个文件夹:notes.md + exercises/ ├── 01-01-asynchronous-tasks-with-django-and/ ├── 02-02-python-celery-basics/ ├── 03-03-why-use-celery/ ├── 04-04-how-can-you-leverage-celery/ ├── 05-05-integrate-celery-with-django/ ├── 06-06-set-up-the-feedback-app/ ├── 07-07-install-celery-as-your-task/ ├── 08-08-install-redis-as-your-celery/ ├── 09-09-add-celery-to-your-django/ ├── 10-10-handle-workloads-asynchronously-with-celery/ ├── 11-11-revisit-the-synchronous-code/ ├── 12-12-refactor-the-code-as-a/ ├── 13-13-test-your-asynchronous-task/ ├── 14-14-conclusion/ └── 15-15-frequently-asked-questions/ - 每课文件夹里建 notes.md(顶部写本课官方链接)和 exercises/;课节文件夹名称与上面一致。 - 已有课程目录先检查并复用;不覆盖笔记、作业或 progress.md,不重置已有进度。尚未发布的课节保留入口并注明未发布,不能代写讲义或虚构练习。 4. 自检 - 创建 check-env.sh,逐项检查并记录“通过/未通过”: - 在课程根目录运行;每项检查独立执行,进入子目录不能影响下一项。使用当前系统的终端运行。 - redis-cli ping → 返回 PONG,说明 Redis 已启动。 - celery -A 项目名 worker -l INFO → worker 正常启动并显示已连接 broker,等待接收任务。 - 在浏览器提交反馈表单 → 页面迅速返回,worker 终端出现该任务的执行日志。 - 标注为人工检查、浏览器操作或 notebook 单元的项目,按相应方式操作,不直接粘贴进 shell;脚本只包含当前系统可执行的命令。 - 仅在当前路线需要、对应材料已获准保存、工具已安装时运行该项;需要登录、付费或尚未开放的材料记为“需要我操作/未验证”,不能伪报通过。不执行会调用付费 API、提交作业或修改云服务的检查。 - 运行一次,把结果给我看;未通过的项目说明原因和修复办法。 5. 学习计划 - README.md:课程入口、课节清单(带链接)、每次学习怎么开始(使用“每课提示词”)。 - progress.md:每课三项勾选——读讲义、做练习、记笔记。 6. 最后分三部分汇报:已完成、需要我自己操作的、尚未验证的。 边界: - 需要管理员权限、会产生费用或需要填写密钥的操作,先告诉我原因,等我同意。密钥由我在本机配置,不发到聊天,不写进笔记或版本库;原项目需要 .env 时确认已被 Git 忽略。 - 不替我写练习或作业答案,也不替我提交。我卡住时只给提示;我明确要求时才给解答。 - 只下载许可允许个人保存的材料;其余只放链接。不把未运行的命令说成成功。 如果你不能直接操作我的文件系统,请在确认目录后给出适用于我的系统、可直接执行的命令和需要保存的文件内容。搭好后的课程目录
django-celery-async-tasks/ ├── README.md # 课程入口、课节清单、每次怎么开始 ├── progress.md # 每课:读讲义 / 做练习 / 记笔记 ├── questions.md # 待解决的问题 ├── check-env.sh # 环境自检,按 README 操作 ├── data/ # 练习用到的样例数据 └── lessons/ # 每课一个文件夹:notes.md + exercises/ ├── 01-01-asynchronous-tasks-with-django-and/ ├── 02-02-python-celery-basics/ ├── 03-03-why-use-celery/ ├── 04-04-how-can-you-leverage-celery/ ├── 05-05-integrate-celery-with-django/ ├── 06-06-set-up-the-feedback-app/ ├── 07-07-install-celery-as-your-task/ ├── 08-08-install-redis-as-your-celery/ ├── 09-09-add-celery-to-your-django/ ├── 10-10-handle-workloads-asynchronously-with-celery/ ├── 11-11-revisit-the-synchronous-code/ ├── 12-12-refactor-the-code-as-a/ ├── 13-13-test-your-asynchronous-task/ ├── 14-14-conclusion/ └── 15-15-frequently-asked-questions/
来源与使用须知
- 原始资源
- Real Python
- 收录时间
- 本站首次收录 2026-10-20
- 指南参考
- 课程主页
版权与转载
- 课程材料未标明许可:仅供个人学习,转载、修改或商用前请先联系原作者。
- 本站只提供原始链接和学习指南,不转载、不镜像课程材料;引用时请注明原作者与出处。