How to stop a Backgroundtask from SocketIO? /u/SatooYT Python Education

So I am currently trying to use Websocket so I can have a livestream from my ESP32-Cam to a React-Frontend. The current problem is that i cant find any solution to kill the background task that sends me messages in a loop (will later be replaced with images) and even after I terminate the running code in Pycharm my the code doesnt even stop and the Port is used up :/. Can somebody please help me. Not even ChatGPT has an answer for me and I dont know what to do now.

import eventlet import socketio from socketio import client eventlet.monkey_patch() import time from flask import Flask, request, jsonify from flask_cors import CORS from flask_socketio import SocketIO,emit from io import BytesIO from PIL import Image, UnidentifiedImageError app = Flask(__name__) CORS(app) socketio = SocketIO(app,cors_allowed_origins="*") clients = {} stream_on = False def send_livestream_data(): """This function runs in the background and sends data to the client every second.""" global stream_on while True: if stream_on: print("hello") socketio.emit('message', {'data': 'Hellooooo'}) else: break @socket.on('connect') def handle_connect(): sid = request.sid clients[sid] = {"status": "connected"} print(f"Client connected with SID: {sid}") socketio.emit('message', {'data': f'Connected with SID: {sid}'}) @socket.on('message') def handle_message(data): """event listener when client types a message""" print("Message from Client: ",data) @socket.on('start') def handle_start(data): print("Sent to start: ", data) global stream_on stream_on = True socketio.start_background_task(send_livestream_data) @socket.on('stop') def handle_stop(data): print("Sent to stop: ", data) global stream_on stream_on = False @socket.on('disconnect') def handle_disconnect(): sid = request.sid # Get the session ID if sid in clients: del clients[sid] # Remove the session ID from the dictionary print(f"Client disconnected with SID: {sid}") if __name__ == '__main__': socketio.run(app, debug=True,port=5002) 

submitted by /u/SatooYT
[link] [comments]

​r/learnpython So I am currently trying to use Websocket so I can have a livestream from my ESP32-Cam to a React-Frontend. The current problem is that i cant find any solution to kill the background task that sends me messages in a loop (will later be replaced with images) and even after I terminate the running code in Pycharm my the code doesnt even stop and the Port is used up :/. Can somebody please help me. Not even ChatGPT has an answer for me and I dont know what to do now. import eventlet import socketio from socketio import client eventlet.monkey_patch() import time from flask import Flask, request, jsonify from flask_cors import CORS from flask_socketio import SocketIO,emit from io import BytesIO from PIL import Image, UnidentifiedImageError app = Flask(__name__) CORS(app) socketio = SocketIO(app,cors_allowed_origins=”*”) clients = {} stream_on = False def send_livestream_data(): “””This function runs in the background and sends data to the client every second.””” global stream_on while True: if stream_on: print(“hello”) socketio.emit(‘message’, {‘data’: ‘Hellooooo’}) else: break @socket.on(‘connect’) def handle_connect(): sid = request.sid clients[sid] = {“status”: “connected”} print(f”Client connected with SID: {sid}”) socketio.emit(‘message’, {‘data’: f’Connected with SID: {sid}’}) @socket.on(‘message’) def handle_message(data): “””event listener when client types a message””” print(“Message from Client: “,data) @socket.on(‘start’) def handle_start(data): print(“Sent to start: “, data) global stream_on stream_on = True socketio.start_background_task(send_livestream_data) @socket.on(‘stop’) def handle_stop(data): print(“Sent to stop: “, data) global stream_on stream_on = False @socket.on(‘disconnect’) def handle_disconnect(): sid = request.sid # Get the session ID if sid in clients: del clients[sid] # Remove the session ID from the dictionary print(f”Client disconnected with SID: {sid}”) if __name__ == ‘__main__’: socketio.run(app, debug=True,port=5002) submitted by /u/SatooYT [link] [comments] 

So I am currently trying to use Websocket so I can have a livestream from my ESP32-Cam to a React-Frontend. The current problem is that i cant find any solution to kill the background task that sends me messages in a loop (will later be replaced with images) and even after I terminate the running code in Pycharm my the code doesnt even stop and the Port is used up :/. Can somebody please help me. Not even ChatGPT has an answer for me and I dont know what to do now.

import eventlet import socketio from socketio import client eventlet.monkey_patch() import time from flask import Flask, request, jsonify from flask_cors import CORS from flask_socketio import SocketIO,emit from io import BytesIO from PIL import Image, UnidentifiedImageError app = Flask(__name__) CORS(app) socketio = SocketIO(app,cors_allowed_origins="*") clients = {} stream_on = False def send_livestream_data(): """This function runs in the background and sends data to the client every second.""" global stream_on while True: if stream_on: print("hello") socketio.emit('message', {'data': 'Hellooooo'}) else: break @socket.on('connect') def handle_connect(): sid = request.sid clients[sid] = {"status": "connected"} print(f"Client connected with SID: {sid}") socketio.emit('message', {'data': f'Connected with SID: {sid}'}) @socket.on('message') def handle_message(data): """event listener when client types a message""" print("Message from Client: ",data) @socket.on('start') def handle_start(data): print("Sent to start: ", data) global stream_on stream_on = True socketio.start_background_task(send_livestream_data) @socket.on('stop') def handle_stop(data): print("Sent to stop: ", data) global stream_on stream_on = False @socket.on('disconnect') def handle_disconnect(): sid = request.sid # Get the session ID if sid in clients: del clients[sid] # Remove the session ID from the dictionary print(f"Client disconnected with SID: {sid}") if __name__ == '__main__': socketio.run(app, debug=True,port=5002) 

submitted by /u/SatooYT
[link] [comments] 

Leave a Reply

Your email address will not be published. Required fields are marked *