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/Player.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'swiftstory/Player.py') diff --git a/swiftstory/Player.py b/swiftstory/Player.py index 786569c..7193b64 100644 --- a/swiftstory/Player.py +++ b/swiftstory/Player.py @@ -17,9 +17,9 @@ class Player: def pop_card(self, card_id): return self.cards.pop(card_id) - async def inc_score(self): + def inc_score(self): self.score += 1 - await self.send_notification({ + self.register_notification({ 'op': 'updated_score', 'content': self.score, }) @@ -29,10 +29,10 @@ class Player: self.next_idx += 1 return self.next_idx - 1 - async def send_notification(self, obj): + def register_notification(self, obj): if self.client is None: return message = json.dumps({'type': 'notification', 'content': obj}) - await self.client.send_notification(message) + self.client.register_notification(message) -- cgit v1.2.3