How can I start a SocketIO server and have it be listening in the background while I continue to execute code on the main thread?
Right now I am using the package python-socketio
. I have also testedflask-socketio
which using python-socketio anyway.
https://python-socketio.readthedocs.io/en/latest/server.html
What I've mostly tried is starting the server with sio.start_background_task
.
For example:
class SocketIOServer(socketio.AsyncNamespace):
def __init__(self):
super().__init__()
self.sio = socketio.AsyncServer()
self.sio.register_namespace(self)
self.app = web.Application()
self.sio.attach(self.app)
self.task = None
def run(self):
web.run_app(self.app, port=8080)
def start(self):
self.task = self.sio.start_background_task(self.run)
I tried the above and multiple variations like using Flask, Tornado, etc.
To be more clear this is basically what I want to do:
if __name__ == '__main__':
# ...
# start app, e.g. -> web.run_app(self.app, port=8080)
# I want to continue to execute code here
I don't fully understand how everything is working, am I asking a stupid question?