feat: test_to_thread

This commit is contained in:
martsforever
2025-09-07 00:02:44 +08:00
parent c312a15e3e
commit 48291e62a6
+36
View File
@@ -0,0 +1,36 @@
import asyncio
import time
async def main():
common_counter = 0
def sync_delay(name: str):
nonlocal common_counter
# current_counter = common_counter
time.sleep(0.5)
print(f"{name},111\n")
print(f"{name},222\n")
common_counter = common_counter + 1
print(f"{name},333\n")
print(f"{name}[common_counter]:{common_counter}\n")
print(f"{name},444\n")
start_time = time.time()
# sync_delay("1")
# 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")),
)
print("common_counter", common_counter)
print(f"耗时: {time.time() - start_time}s")
asyncio.run(main())