I am working on code similar to below code. Sometimes the program stops working or I get strange errors regarding socketio session access. Slowly I feel it could be race conditions.
Its more pseudo code. I want to demonstrate, that I access global shared state and the socketio sessions from multiple coroutines.
import asyncio as aio
from aiohttp import web
import socketio
app = web.Application()
sio = socketio.AsyncServer()
app["sockets"] = []
@sio.on("connect")
async def connect(sid):
app["sockets"].append(sid)
@sio.on("disconnect")
async def disconnect(sid):
app["sockets"].remove(sid)
@sio.on("set session")
async def set_session(sid, arg):
await sio.save_session(sid, {"arg": arg})
async def session_route(req):
data = await req.json()
for sid in app["sockets"]:
await sio.save_session(sid, {"arg": data["arg"]})
return web.Response(status=200)
if __name__ == '__main__':
web.run_app(app)