Hello, this is the first time I have used sanic on a company project, the sanic framework is very good!
Now I have a problem. I want to persist a websocket connection,in order to send request to operator websocket to send messages. When workers=1, I can get the value, but when workers>1, I often can’t get it. I guess it may be that different workers do not share these variables. , So is there any way I can get the value when worker>1 ?looking forward to your answers
this my some codes
@app.get("/send_msg/<msg:str>")
async def send_msg(request, msg: str):
await app.ctx.current_ws.send(msg)
return json({"code": 200, "msg": "success"})
@app.websocket("/feed")
async def feed(request, ws):
app.ctx.current_ws = ws
while True:
data = "hello!"
print("Sending: " + data)
await ws.send(data)
data = await ws.recv()
print("Received: " + data)
when I send request http://127.0.0.1/send_msg/something, many time it show “AttributeError: ‘types.SimpleNamespace’ object has no attribute ‘current_ws’”,some time ok