Created
July 18, 2024 17:43
-
-
Save agronholm/0d374e4de116715495dd6faafe71b512 to your computer and use it in GitHub Desktop.
Testing Lock implementation performance differences with AnyIO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import time | |
import trio | |
import anyio | |
async def acquire_release(lock): | |
async with lock: | |
await anyio.sleep(0) | |
async def main(lock_class: type): | |
lock = lock_class() | |
async with anyio.create_task_group() as tg: | |
for _ in range(10000): | |
tg.start_soon(acquire_release, lock) | |
def benchmark(lock_class: type, name: str, backend: str) -> None: | |
start = time.monotonic() | |
anyio.run(main, lock_class, backend=backend) | |
print(f"{name}: {time.monotonic() - start}") | |
benchmark(anyio.Lock, "anyio+asyncio", backend="asyncio") | |
benchmark(asyncio.Lock, "asyncio", backend="asyncio") | |
benchmark(anyio.Lock, "anyio+trio", backend="trio") | |
benchmark(trio.Lock, "trio", backend="trio") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment