推荐学习书目
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 · Mar 1, 2018 · 2918 views
    This topic created in 3021 days ago, the information mentioned may be changed or developed.

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

    7 replies    2018-03-02 09:54:00 +08:00
    xpresslink
        1
    xpresslink  
       Mar 1, 2018
    没太懂你这个需求,
    前端向后端请求数据直接用 ajax 本身就是异步吧还用得着 celery ?
    只有前端扔给后端一个异步任务比如生成一个文件,等生成好推消息给前端。让用户去下载。
    这个用 celery 的 Task Singals 来写一段 callback 去实现呗。
    cenphoenix
        2
    cenphoenix  
       Mar 1, 2018
    用 Group 是否可行?用户登录进来的时候根据用户 id 创建只有他的 Group 里,异步完成后也是根据这个 id 用 Group 进行通知
    yuhuang
        3
    yuhuang  
    OP
       Mar 1, 2018
    @xpresslink 我的需求是这样的 https://s1.ax1x.com/2018/03/01/9reLlR.png ,现在的问题是怎么知道 celery 任务完成了,并且通知浏览器,这块不清楚怎么实现
    yuhuang
        4
    yuhuang  
    OP
       Mar 1, 2018
    @xpresslink 我来研究一下 celery 的 Task Singals,感谢
    cenphoenix
        5
    cenphoenix  
       Mar 1, 2018
    @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
       Mar 2, 2018
    @cenphoenix 我用的是 Group 组的,现在是我在 Views 里面触发 task,不知道该如何给 channels 发消息,task 里面怎么去连前端进来的这个 Group 组里,并发消息发送出去
    yuhuang
        7
    yuhuang  
    OP
       Mar 2, 2018
    @cenphoenix 是不是我思路错了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1108 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 18:15 · PVG 02:15 · LAX 11:15 · JFK 14:15
    ♥ Do have faith in what you're doing.