-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to close websockets coroutines ? #754
Comments
Do you have any error? Because your code looks good, since you are using |
It hanks, no error available. If I kill the program, I get :
|
When closing websockets I got multiple errors in my code :
My code which opens websockets is : self._ws_connection['peer'] = peer_websocket.connect()
async with self._ws_connection['peer'] as ws:
logging.debug("Connected successfully to peer ws : {0}".format(self.pubkey[:5]))
async for msg in ws:
if msg.tp == aiohttp.MsgType.text:
logging.debug("Received a peer : {0}".format(self.pubkey[:5]))
peer_data = peer_websocket.parse_text(msg.data)
self.refresh_peer_data(peer_data)
elif msg.tp == aiohttp.MsgType.closed:
break
elif msg.tp == aiohttp.MsgType.error:
break And I close it this way : def close_ws(self):
for ws in self._ws_connection.values():
if ws:
asyncio.as_completed(ws.close(), timeout=15) I suppose there is something I'm doing wrong ? |
I fixed a lot on things in my code, and now I'm left with this last problem : I now handle the websocket coroutines instances like this : @asyncify
async def connect_current_block(self):
if not self._ws_connection['block']:
try:
conn_handler = self.endpoint.conn_handler()
block_websocket = bma.ws.Block(conn_handler)
ws_connection = block_websocket.connect()
async with ws_connection as ws:
self._ws_connection['block'] = ws
logging.debug("Connected successfully to block ws : {0}".format(self.pubkey[:5]))
async for msg in ws:
if msg.tp == aiohttp.MsgType.text:
logging.debug("Received a block : {0}".format(self.pubkey[:5]))
block_data = block_websocket.parse_text(msg.data)
await self.refresh_block(block_data)
elif msg.tp == aiohttp.MsgType.closed:
break
elif msg.tp == aiohttp.MsgType.error:
break I close my application with the following : try:
loop.run_until_complete(app.stop())
except asyncio.CancelledError:
logging.info('CancelledError') App.stop closes successfully my code coroutines but not the websockets : async def close_ws(self):
for ws in self._ws_connection.values():
if ws:
await ws.close()
logging.debug("closed") The await passes, I get "closed" printed as expected, but when my application exits, it appears that multiples tasks would not be stopped :
It looks like the async with self._ws_connection['peer'] as ws: I tried to put breakpoints in the Do you have any example of closing websockets from outside the |
Your task running After running |
is this still relevant? actually, this is interesting question. lets say we have task that reads incoming messages, i think we need to introduce new message type |
close() is safe now. |
Due to improvements done in aio-libs#754, websocket `close()` can now be called from a separate task.
* Update server-side websocket close FAQ Due to improvements done in #754, websocket `close()` can now be called from a separate task. * Minor doc fix for websocket receive * Fix web module usage and use .gather * Use set() instead of dict in FAQ
Hello,
I'm running the following code which hangs :
What am I doing wrong and how am I supposed to close websockets sessions ?
I also tried the following, which resulted in the same behaviour :
Thanks a lot for your great library.
The text was updated successfully, but these errors were encountered: