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)