I’m using the websockets package on python and I’m opening a connection as a client,
but NOT in a block context like this:
async with websockets.connect(...) as websocket: ...
I’m opening the connection like this:
websocket = await websockets.connect(uri)
The thing is that I don’t under stand what is the right way to close the connection.
The document explains here about await close()
and await wait_closed()
, but I don’t understand the differences.
Does both are fine?
Should I use both?
If someone would share his experience it would be much appreciated.
Advertisement
Answer
The wait_closed
is used to handle connection termination. And the close
requests a connection termination and waits until the connection is closed.
So if you want to close the connection you should use only await close()
.