写点什么

Django 操作异步任务

作者:乌龟哥哥
  • 2023-05-03
    河北
  • 本文字数:2314 字

    阅读完需:约 8 分钟

前置条件



| 1 2 3 4 5 | ```Python==3.7.0 Pip==3 Django==3.2 celery==5.0.5 redis==3.5.3


| ------------------ | -------------------------------------------------------------------- |
### [](https://cywhat.cn/Django%E6%93%8D%E4%BD%9C%E5%BC%82%E6%AD%A5%E4%BB%BB%E5%8A%A1/#1%E5%AE%89%E8%A3%85)1、安装
****
| ```1 ``` | ```pip3 install celery ``` || ---------- | ---------------------------- |
### [](https://cywhat.cn/Django%E6%93%8D%E4%BD%9C%E5%BC%82%E6%AD%A5%E4%BB%BB%E5%8A%A1/#2%E7%9B%AE%E5%BD%95)2、目录
****
| `````` | `````` || ------- | ------- |
#### [](https://cywhat.cn/Django%E6%93%8D%E4%BD%9C%E5%BC%82%E6%AD%A5%E4%BB%BB%E5%8A%A1/#21%E4%BF%AE%E6%94%B9__init__py%E9%85%8D%E7%BD%AE)2.1、修改`__init__.py`配置
****
| ```1 2 3 4 ``` | ```# 加入如下配置 from .celery import app as celery_app __all__ = ('celery_app',) ``` || ---------------- | ---------------------------------------------------------------------------------- |
#### [](https://cywhat.cn/Django%E6%93%8D%E4%BD%9C%E5%BC%82%E6%AD%A5%E4%BB%BB%E5%8A%A1/#22%E6%96%B0%E5%A2%9E%E6%96%87%E4%BB%B6celerypy)2.2、新增文件`celery.py`
****
| `````` | `````` || ------- | ------- |
#### [](https://cywhat.cn/Django%E6%93%8D%E4%BD%9C%E5%BC%82%E6%AD%A5%E4%BB%BB%E5%8A%A1/#23%E4%BF%AE%E6%94%B9settings)2.3、修改`settings`
**注意**
****
| `````` | `````` || ------- | ------- |
### [](https://cywhat.cn/Django%E6%93%8D%E4%BD%9C%E5%BC%82%E6%AD%A5%E4%BB%BB%E5%8A%A1/#3%E6%96%B0%E5%A2%9Etask)3、新增task
**注意**
复制代码




| 1 2 3 4 5 6 7 8 9 10 | ```from celery import Celery # 专属于 myproject 项目的任务 app = Celery('Heng_Tools') @app.task def test(): pass


| -------------------------------------- | -------------------------------------------------------------------------------------------------------------------- |
****
| `````` | `````` || ------- | ------- |
****
| ```1 ``` | ```装饰器`@shared_task`可以让我们避免对某个项目名对应Celery实例的依赖,使app的可移植性更强。 ``` || ---------- | ----------------------------------------------------------------- |
### [](https://cywhat.cn/Django%E6%93%8D%E4%BD%9C%E5%BC%82%E6%AD%A5%E4%BB%BB%E5%8A%A1/#4%E8%BF%90%E8%A1%8C)4、运行
****
| ```1 ``` | ```Celery -A Heng_Tools worker -l info ``` || ---------- | ------------------------------------------------- |
****
| ```1 2 3 ``` | ```# 如果看到这行就说明启动成功了 [2023-04-18 15:27:03,191: INFO/MainProcess] celery@cywhat ready. ``` || -------------- | ------------------------------------------------------------------------------------------- |
### [](https://cywhat.cn/Django%E6%93%8D%E4%BD%9C%E5%BC%82%E6%AD%A5%E4%BB%BB%E5%8A%A1/#5%E8%B0%83%E7%94%A8%E5%BC%82%E6%AD%A5%E4%BB%BB%E5%8A%A1%E4%BF%A9%E7%A7%8D%E4%BB%BB%E5%8A%A1%E9%83%BD%E4%BC%9A%E8%BF%94%E5%9B%9E%E4%B8%80%E4%B8%AAtaskid)5、调用异步任务[俩种任务都会返回一个taskId]
#### [](https://cywhat.cn/Django%E6%93%8D%E4%BD%9C%E5%BC%82%E6%AD%A5%E4%BB%BB%E5%8A%A1/#51%E8%B0%83%E7%94%A8%E6%96%B9%E6%B3%951)5.1、调用方法1
****
| ```1 ``` | ```result = add.delay(3, 5) ``` || ---------- | --------------------------------- |
#### [](https://cywhat.cn/Django%E6%93%8D%E4%BD%9C%E5%BC%82%E6%AD%A5%E4%BB%BB%E5%8A%A1/#51%E8%B0%83%E7%94%A8%E6%96%B9%E6%B3%952)5.1、调用方法2
****
| ```1 2 ``` | ```# apply_async方法,与delay类似,但支持更多参数 result = add.apply_async(args=[3, 5]) ``` || ------------ | ------------------------------------------------------------------------------- |
### [](https://cywhat.cn/Django%E6%93%8D%E4%BD%9C%E5%BC%82%E6%AD%A5%E4%BB%BB%E5%8A%A1/#6%E5%AE%89%E8%A3%85flower%E7%9B%91%E6%8E%A7)6、安装flower监控
****
| ```1 2 3 4 5 ``` | ```# 安装 pip3 install flower # 运行 celery -A Heng_Tools flower ``` || ------------------ | --------------------------------------------------------------------- |
### [](https://cywhat.cn/Django%E6%93%8D%E4%BD%9C%E5%BC%82%E6%AD%A5%E4%BB%BB%E5%8A%A1/#7%E5%BC%82%E6%AD%A5%E4%BB%BB%E5%8A%A1%E7%9A%84%E4%B8%80%E4%BA%9B%E6%93%8D%E4%BD%9C)7、异步任务的一些操作
****
| ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ``` | ```# 查看task的任务id result.task_id # 查看task的任务状态 result.status # 获取task的结果 AsyncResult(result.task_id).result # 获取task的状态 AsyncResult(result.task_id).result # 取消正在进行中的task任务 AsyncResult(result.task_id).revoke(terminate=True)``` || -------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
复制代码


发布于: 刚刚阅读数: 3
用户头像

乌龟哥哥

关注

正在努力寻找offer的大四小菜鸟 2021-03-16 加入

擅长 Hbuilder、VS Code、MyEclipse、AppServ、PS 等软件的安装与卸载 精通 Html、CSS、JavaScript、jQuery、Java 等单词的拼写 熟悉 Windows、Linux、 等系统的开关机 看–时间过得多快,不说了,去搬砖了

评论

发布
暂无评论
Django操作异步任务_三周年连更_乌龟哥哥_InfoQ写作社区