django 问题咨询 - V2EX
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 2528 次点击
    这是一个创建于 2790 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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     Solana     2953 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 28ms UTC 00:21 PVG 08:21 LAX 17:21 JFK 20:21
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86