从SocketIO连接事件调用Celery任务

huangapple go评论71阅读模式
英文:

Calling Celery task from SocketIO connect event

问题

我想在 WebSocket 客户端连接到 socket 服务器时调用我的 Celery 任务当我尝试在连接事件中调用它时会导致应用超时客户端无法接收到 emit

以下是应用程序的代码示例

```python
from flask import Flask
from flask_socketio import SocketIO
import eventlet
from celery import Celery
import time

eventlet.monkey_patch(socket=True)
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret'
socketio = SocketIO(app, async_mode='eventlet', logger=True, engineio_logger=True, message_queue='redis://127.0.0.1:6379' )

celery = Celery(app.name, broker='redis://127.0.0.1:6379')
celery.conf.update(app.config)

@app.route('/')
def home():
    return 'Hello World!'

@socketio.on('connect')
def connect():
    print('客户端已连接,正在调用 Celery 任务...')
    celeryTask(1, 2)

@celery.task()
def celeryTask(x, y):
    print('调用 Celery 任务!')
    local_socketio = SocketIO(app, logger=True, engineio_logger=True, message_queue='redis://127.0.0.1:6379')
    while True:
        local_socketio.emit('add', {'data': x + y})
        time.sleep(60)

if __name__ == '__main__':
    socketio.run(app, debug=True)
英文:

I would like to call my celery task when a websocket client connects to the socket server. When I try calling it from the connect event it causes the application to time out and the client does not receive the emit.

Below is an example of the code for the application:

from flask import Flask
from flask_socketio import SocketIO
import eventlet
from celery import Celery
import time

eventlet.monkey_patch(socket=True)
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret'
socketio = SocketIO(app, async_mode='eventlet', logger=True, engineio_logger=True, message_queue='redis://127.0.0.1:6379' )

celery = Celery(app.name, broker='redis://127.0.0.1:6379')
celery.conf.update(app.config)

@app.route('/')
def home():
    return 'Hello World!'

@socketio.on('connect')
def connect():
    print('Client connected, calling celery task...')
    celeryTask(1,2)

@celery.task()
def celeryTask(x,y):
    print('Celery task called!')
    local_socketio = SocketIO(app, logger=True, engineio_logger=True, message_queue='redis://127.0.0.1:6379')
    while True:
        local_socketio.emit('add', {'data': x+y})
        time.sleep(60)

if __name__ == '__main__':
    socketio.run(app, debug=True)

Any help would be greatly appreciated!

答案1

得分: 1

@celery.task()
def celeryTask(x, y):
    print('Celery任务被调用!')
    local_socketio = SocketIO(logger=True, engineio_logger=True, message_queue='redis://127.0.0.1:6379')
    while True:
        local_socketio.emit('add', {'data': x+y})
        time.sleep(60)
英文:

The socketio instance that you are using in your Celery task should not be initialized with the app instance from Flask. This isn't a web server, is just an auxiliary emitter.

@celery.task()
def celeryTask(x,y):
    print('Celery task called!')
    local_socketio = SocketIO(logger=True, engineio_logger=True, message_queue='redis://127.0.0.1:6379')
    while True:
        local_socketio.emit('add', {'data': x+y})
        time.sleep(60)

If that does not work, you will need to add logs to your question, as that provide more clues.

答案2

得分: 0

也许尝试将 @celery.task() 放在 @socketio.on('connect') 之前。可能会有帮助。

英文:

Maybe try to put @celery.task() before @socketio.on('connect'). That might help.

huangapple
  • 本文由 发表于 2023年2月19日 08:39:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497301.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定