[Python] How to manually close a websocket

Discussão em 'Python' iniciado por Stack, Agosto 24, 2025.

  1. Stack

    Stack Membro Participativo

    If I'm not using a context for a websocket, how would I manually close it? When I try running conn.close() I get an event loop is closed error:

    from asgiref.sync import async_to_sync
    import websockets

    # connect -- works OK
    conn = async_to_sync(websockets.connect)("ws://localhost:8000/ws/registration/123/")

    # send message -- works OK
    async_to_sync(conn.send)("hello")

    # disconnect -- doesn't work
    async_to_sync(conn.close)()

    Continue reading...

Compartilhe esta Página