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
gogobody
V2EX  ›  Python

请教在 celery 中运行 flask socket-io 的问题!

  •  
  •   gogobody · 2017-11-29 01:10:52 +08:00 · 3135 次点击
    这是一个创建于 2312 天前的主题,其中的信息可能已经有所发展或是发生改变。

    项目结构如下:

    website/
    	celery/
        	task.py
        socketio/
        	socket.py
        views/
        	index.py
        init.py
    manage.py
    
    

    我的 flask app 在 init 里初始化,celery 在 task 里初始化,socket 在 socket.py 里初始化。 然后在 index 里创建一个路由

    @app.route('/index')
    def indexx():
        from website.celery.tasks import test
        test.delay()
        return 'a'
    

    test 如下:

    from website.socketio.socket import socket
    @celery.task
    def test():
        # socketio = SocketIO(message_queue='redis://localhost:6379/1')
        socketio.emit('response',{'data':'sdasda'},broadcast=True)
        print "aaaa"
    

    然后运行,celery shell 显示任务执行了,但是 socket 发的消息并没有在前端收到怎么办呢?好急 请教各位大神啦!

    9 条回复    2017-12-01 10:22:06 +08:00
    ericls
        1
    ericls  
       2017-11-29 04:11:06 +08:00 via iPhone
    并不是同一个 socket
    ericls
        2
    ericls  
       2017-11-29 04:11:50 +08:00 via iPhone
    Socket 的后端用的是什么?
    gogobody
        3
    gogobody  
    OP
       2017-11-29 09:29:01 +08:00 via Android
    @ericls 大神求助,后端是 flask-socketio,celery 中的 socket 实例是从 socket.py 里面 import 进来的
    ericls
        4
    ericls  
       2017-11-29 10:03:24 +08:00 via iPhone
    @gogobody 因为 celery 和 web 是分开的 process 所以你需要有一个共同的后端……

    手机看了一下文档 If using multiple processes, a message queue service is used by the processes to coordinate operations such as broadcasting. The supported queues are Redis, RabbitMQ, and any other message queues supported by the Kombu package.
    gogobody
        5
    gogobody  
    OP
       2017-11-29 10:32:03 +08:00
    @ericls 我的 socket 是这样初始化的 socketio = SocketIO(app) socketio.run(app),celery 是使用 redis://localhost:6379/0,尝试 socketio = SocketIO(app,message_queue="redis://localhost:6379/0")这样初始化,结果初始化后连接不到 socket 了
    ycz0926
        6
    ycz0926  
       2017-11-30 16:16:25 +08:00   ❤️ 1
    你可以在 index.py 里起一个 route,这个 route 里执行 emit,test 里直接 request 这个 route 就可以了
    gogobody
        7
    gogobody  
    OP
       2017-11-30 18:21:29 +08:00 via Android
    @ycz0926 这样相当于中转了一下,确实可行哈,感谢
    ycz0926
        8
    ycz0926  
       2017-12-01 00:18:50 +08:00
    @gogobody no thanks,我最近也在弄这个,有另一种实现方式 https://github.com/miguelgrinberg/Flask-SocketIO/issues/47
    gogobody
        9
    gogobody  
    OP
       2017-12-01 10:22:06 +08:00 via Android
    @ycz0926 居然是 3 年前的问题了😂
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5488 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 06:40 · PVG 14:40 · LAX 23:40 · JFK 02:40
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.