#๐Ÿ”’ Continue the post of websocket in quart

10 messages ยท Page 1 of 1 (latest)

sharp sage
grim arrowBOT
#

@sharp sage

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

sharp sage
#

i have tried the TaskGroup and it said websocket is not defined

 ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
  +-+---------------- 1 ----------------
    | Traceback (most recent call last):
    |   File "C:\Users\Asher\Desktop\Vocard_Dashboard\objects.py", line 42, in recv_loop
    |     message = await websocket.receive()
    |                     ^^^^^^^^^
    | NameError: name 'websocket' is not defined
#

main.py

@app.websocket("/ws_user")
@login_required
async def ws_user(user: User):
    try:
        # await user.connect(websocket)
        async with asyncio.TaskGroup() as tg:
            tg.create_task(user.recv_loop())
            tg.create_task(user.send_loop())
    except asyncio.CancelledError:
        # await user.disconnect()
        raise
#

object.py

class User:
  ...

  async def recv_loop(self) -> None:
      while True:
          message = await websocket.receive()
          print(message)
  
  def send_nowait(self, message):
      # Call this from Bot and vice versa
      self._queue.put_nowait(message)

  async def send_loop(self):
      while True:
          message = await self._queue.get()
          await websocket.send(message)
soft abyss
sharp sage
#

oh i thought it was a contextual variable

#

but after i defined the websocket the websocket will got replaced with a new websocket

grim arrowBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.