V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
yuhuang
V2EX  ›  Python

django 问题咨询

  •  
  •   yuhuang · 2018-03-01 11:37:40 +08:00 · 1999 次点击
    这是一个创建于 2241 天前的主题,其中的信息可能已经有所发展或是发生改变。

    django 里面如何让 celery 的 task 产生数据后通知 channels 把数据传给前端,麻烦大佬指点一下

    7 条回复    2018-03-02 09:54:00 +08:00
    xpresslink
        1
    xpresslink  
       2018-03-01 14:22:26 +08:00
    没太懂你这个需求,
    前端向后端请求数据直接用 ajax 本身就是异步吧还用得着 celery ?
    只有前端扔给后端一个异步任务比如生成一个文件,等生成好推消息给前端。让用户去下载。
    这个用 celery 的 Task Singals 来写一段 callback 去实现呗。
    cenphoenix
        2
    cenphoenix  
       2018-03-01 16:40:03 +08:00
    用 Group 是否可行?用户登录进来的时候根据用户 id 创建只有他的 Group 里,异步完成后也是根据这个 id 用 Group 进行通知
    yuhuang
        3
    yuhuang  
    OP
       2018-03-01 16:44:02 +08:00
    @xpresslink 我的需求是这样的 https://s1.ax1x.com/2018/03/01/9reLlR.png ,现在的问题是怎么知道 celery 任务完成了,并且通知浏览器,这块不清楚怎么实现
    yuhuang
        4
    yuhuang  
    OP
       2018-03-01 16:47:11 +08:00
    @xpresslink 我来研究一下 celery 的 Task Singals,感谢
    cenphoenix
        5
    cenphoenix  
       2018-03-01 17:54:42 +08:00
    @yuhuang 你在 task 里面调用 Group 进行通知不行吗?象这样

    @celery_app.task
    def send_phone_code(user_id, verify_token, phone_number):
    customer = Customer.objects.get(id=user_id)
    #发送短信操作
    send_message(...)
    try:
    # 通知任务已完成
    Group('phone_verify-%s' % customer.username).send({
    'text': json.dumps({
    'success': True,
    'msg': 'new message sent'
    })
    })
    except Exception as e:
    # 通知任务失败
    Group('phone_verify-%s' % customer.username).send({
    'text': json.dumps({
    'success': False,
    'msg': e.msg
    })
    })
    yuhuang
        6
    yuhuang  
    OP
       2018-03-02 09:51:40 +08:00
    @cenphoenix 我用的是 Group 组的,现在是我在 Views 里面触发 task,不知道该如何给 channels 发消息,task 里面怎么去连前端进来的这个 Group 组里,并发消息发送出去
    yuhuang
        7
    yuhuang  
    OP
       2018-03-02 09:54:00 +08:00
    @cenphoenix 是不是我思路错了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4697 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 03:59 · PVG 11:59 · LAX 20:59 · JFK 23:59
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.