From 5dd8dc02ef3d6de9ffa72e5ea5ede29ab83c4d83 Mon Sep 17 00:00:00 2001 From: hsahovic <25432355+hsahovic@users.noreply.github.com> Date: Fri, 26 Aug 2022 01:19:57 +0200 Subject: [PATCH] Use create task instead of ensure future --- src/poke_env/player/player_network_interface.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/poke_env/player/player_network_interface.py b/src/poke_env/player/player_network_interface.py index fe89ed6a9..152631b99 100644 --- a/src/poke_env/player/player_network_interface.py +++ b/src/poke_env/player/player_network_interface.py @@ -10,7 +10,7 @@ from abc import ABC from abc import abstractmethod from asyncio import CancelledError -from asyncio import ensure_future +from asyncio import create_task from asyncio import Event from asyncio import Lock from asyncio import sleep @@ -66,6 +66,7 @@ def __init__( If None pings will never time out. :type ping_timeout: float, optional """ + self._active_tasks: set = set() self._ping_interval = ping_interval self._ping_timeout = ping_timeout self._authentication_url = server_configuration.authentication_url @@ -309,7 +310,10 @@ async def listen(self) -> None: self._websocket = websocket async for message in websocket: self.logger.info("\033[92m\033[1m<<<\033[0m %s", message) - ensure_future(self._handle_message(message)) + task = create_task(self._handle_message(message)) + self._active_tasks.add(task) + task.add_done_callback(self._active_tasks.discard) + except websockets.exceptions.ConnectionClosedOK: self.logger.warning( "Websocket connection with %s closed", self.websocket_url