python-websockets / websockets

Library for building WebSocket servers and clients in Python
https://websockets.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
5.07k stars 506 forks source link

websockets.connect error #1378

Closed h-acker closed 1 year ago

h-acker commented 1 year ago

when i run the code:

async def main():
    async with websockets.connect(URL) as websocket:
        await websocket.send(json.dumps({"type":"init", "id":"server"}))
        loop = asyncio.get_running_loop()
        loop.add_signal_handler(
            signal.SIGTERM, loop.create_task, websocket.close())

        async for message in websocket:
            logger.info(message)

if __name__ == '__main__':
    asyncio.run(main())

error:

File "D:\miniconda3\envs\monitor\Lib\asyncio\runners.py", line 118, in run return self._loop.run_until_complete(task) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\miniconda3\envs\monitor\Lib\asyncio\base_events.py", line 653, in run_until_complete return future.result() ^^^^^^^^^^^^^^^ File "E:\video_stream_workspace\monitor\services.py", line 15, in main loop.add_signal_handler( File "D:\miniconda3\envs\monitor\Lib\asyncio\events.py", line 574, in add_signal_handler raise NotImplementedError NotImplementedError sys:1: RuntimeWarning: coroutine 'WebSocketCommonProtocol.close' was never awaited

aaugustin commented 1 year ago

It looks like your environment doesn't support signals. Check the docs for loop.add_signal_handler.

h-acker commented 1 year ago

oh, thanks! win11 is Oops. would you like give me some advice?

aaugustin commented 1 year ago

Just don't set a signal handler.