从后台任务发出flask-socketio不会发出

时间:2020-06-07 20:48:30

标签: python flask-socketio python-socketio

下面是我的代码段,我正在运行一个后台任务,想在调用事件时发出ID。

manage.py

from threading import Lock 

continue_reading = True
thread = None
thread_lock = Lock()

socketio = SocketIO(app, cors_allowed_origins='*', logger=True, engineio_logger=True, **params)

def background_task(app):
    with app.app_context():
        while continue_reading:
            id = setup_rfidreader() 
            logging.debug("chip id from bg {}".format(id)) 
            socketio.emit('chip was readed', {"timestamp": id} )

@socketio.on('read_card')
def handle_read_card_event(): 
    global thread
    with thread_lock:
        if thread is None:
            thread = socketio.start_background_task(background_task, current_app._get_current_object())

后台任务阻止调用其他事件,为什么?

def setup_rfidreader():
    reader = SimpleMFRC522()
    print("Hold a tag near the reader")
    try:
        id, text = reader.read()
        sleep(.5)
        print(id)
        return id
    except KeyboardInterrupt:
        print('interrupted')
    finally:
        GPIO.cleanup()

1 个答案:

答案 0 :(得分:0)

似乎eventlet和redis url配置不正确。