英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论