From fce77d456acb376259c70ecd5facc046797bbbf3 Mon Sep 17 00:00:00 2001 From: Andreas Bergman Date: Wed, 20 Mar 2024 09:12:51 +0100 Subject: [PATCH] Update a bug with sleep functionality that will affect the asyncio performance --- 2024/tuesday_tips/testing_async/fetch_event.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/2024/tuesday_tips/testing_async/fetch_event.py b/2024/tuesday_tips/testing_async/fetch_event.py index d1271806..a190705a 100644 --- a/2024/tuesday_tips/testing_async/fetch_event.py +++ b/2024/tuesday_tips/testing_async/fetch_event.py @@ -1,6 +1,6 @@ import json import random -from time import sleep +import asyncio from aiohttp import ClientSession @@ -8,13 +8,15 @@ async def read_data( path: str = "./data/fictional_events.json", ) -> dict[str, dict[str, str]]: - sleep(random.choice([0.125, 0.5, 0.75, 0.1])) + await asyncio.sleep(random.choice([0.125, 0.5, 0.75, 0.1])) with open(path) as file: data = json.load(file) return data -async def fetch_event(event_id: str, session: ClientSession = None) -> dict[str, str]: +async def fetch_event( + event_id: str, session: ClientSession | None = None +) -> dict[str, str]: json_data = await read_data() try: