From 6d9cafe2eba15ec76e76774b8e144ffe454b2340 Mon Sep 17 00:00:00 2001 From: martsforever Date: Sun, 7 Sep 2025 00:04:35 +0800 Subject: [PATCH] feat: create --- app/utils/test_to_thread.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/app/utils/test_to_thread.py b/app/utils/test_to_thread.py index aeb2b54..7f68d67 100644 --- a/app/utils/test_to_thread.py +++ b/app/utils/test_to_thread.py @@ -5,10 +5,10 @@ import time async def main(): common_counter = 0 - def sync_delay(name: str): + async def async_delay(name: str): nonlocal common_counter # current_counter = common_counter - time.sleep(0.5) + await asyncio.sleep(0.5) print(f"{name},111\n") print(f"{name},222\n") common_counter = common_counter + 1 @@ -22,10 +22,16 @@ async def main(): # sync_delay("2") # sync_delay("3") + # await asyncio.gather( + # asyncio.create_task(asyncio.to_thread(sync_delay, name="A")), + # asyncio.create_task(asyncio.to_thread(sync_delay, name="B")), + # asyncio.create_task(asyncio.to_thread(sync_delay, name="C")), + # ) + await asyncio.gather( - asyncio.create_task(asyncio.to_thread(sync_delay, name="A")), - asyncio.create_task(asyncio.to_thread(sync_delay, name="B")), - asyncio.create_task(asyncio.to_thread(sync_delay, name="C")), + asyncio.create_task(async_delay(name="A")), + asyncio.create_task(async_delay(name="B")), + asyncio.create_task(async_delay(name="C")), ) print("common_counter", common_counter)