Below is my code snippet, I'm running a background task and want to emit the id when the event is being called.
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())
The background task prevent calling other events, why?
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()