From 05235c8df61b0cd6bf588ee15a924576cb10aa0a Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Wed, 27 May 2020 20:50:16 +0200 Subject: Don't use coroutines for functions sending notifications Functions which generate notifications for clients were all making use of await and async. This is not great because if we add a notification somewhere, we need to change the function to a coroutine and update all invocations (recursively changing all functions to coroutines). Instead, we now add a task to the event loop whenever a notification needs to be generated. This allows to drop the await and async specifiers from mostly everywhere. On the downside, it means that if we send a notification to n clients, we have to register n tasks. Signed-off-by: Olivier Gayot --- swiftstory/Client.py | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) (limited to 'swiftstory/Client.py') diff --git a/swiftstory/Client.py b/swiftstory/Client.py index bc347f9..5116f51 100644 --- a/swiftstory/Client.py +++ b/swiftstory/Client.py @@ -1,3 +1,4 @@ +import asyncio import websockets from swiftstory.Status import error @@ -12,7 +13,7 @@ class Client: self.socket = socket self.player = None - async def join_game(self, game_name, lang): + def join_game(self, game_name, lang): if self.game is not None: return error('You are already in a game') @@ -25,27 +26,27 @@ class Client: if game is None: return error('Invalid language') - return await game.try_join(self) + return game.try_join(self) - async def play_white_card(self, card_id): + def play_white_card(self, card_id): if self.game is None: return error('You have to join a game first') - return await self.game.try_play_card(self.player, card_id) + return self.game.try_play_card(self.player, card_id) - async def pick_black_card(self): + def pick_black_card(self): if self.game is None: return error('You have to join a game first') - return await self.game.try_become_judge(self.player) + return self.game.try_become_judge(self.player) - async def collect_cards(self): + def collect_cards(self): if self.game is None: error('You have to join a game first') - return await self.game.try_collect_cards(self.player) + return self.game.try_collect_cards(self.player) - async def designate_card(self, card_id): + def designate_card(self, card_id): if self.game is None: return error('You have to join a game first') - return await self.game.try_designate_card(self.player, card_id) + return self.game.try_designate_card(self.player, card_id) def view_player_cards(self): if self.game is None: @@ -62,12 +63,15 @@ class Client: return error('You have to join a game first') return self.game.try_view_black_card(self.player) - async def send_notification(self, message): - try: - await self.socket.send(message) - except websockets.exceptions.ConnectionClosed: - print("Recipient has disconnected.") + def register_notification(self, message): + async def f(): + try: + await self.socket.send(message) + except websockets.exceptions.ConnectionClosed: + print("Recipient has disconnected.") - async def disconnect(self): + asyncio.create_task(f()) + + def disconnect(self): if self.player is not None: - await self.game.disconnect(self.player) + self.game.disconnect(self.player) -- cgit v1.2.3