Skip to content

Instantly share code, notes, and snippets.

@graingert
Created July 20, 2022 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save graingert/7628f87a629d15cce88f91d6c8cbb1b6 to your computer and use it in GitHub Desktop.
Save graingert/7628f87a629d15cce88f91d6c8cbb1b6 to your computer and use it in GitHub Desktop.
import asyncio
import async_timeout
import quattro
import contextlib
import anyio
async def main(note, cmgr):
try:
async with cmgr(-11):
await asyncio.sleep(0)
except (TimeoutError, asyncio.TimeoutError):
print(f"{note=} PASS: great a timeout is here")
else:
print(f"{note=} FAILED: expected a timeout!")
try:
async with cmgr(-11):
await asyncio.sleep(0)
await asyncio.sleep(0)
except (TimeoutError, asyncio.TimeoutError):
print(f"{note=} PASS: great a timeout is here")
else:
print(f"{note=} FAILED: expected a timeout!")
@contextlib.asynccontextmanager
async def native(delay):
async with asyncio.timeout(delay):
yield
@contextlib.asynccontextmanager
async def quattro_(delay):
with quattro.fail_after(delay):
yield
@contextlib.asynccontextmanager
async def anyio_(delay):
with anyio.fail_after(delay):
yield
@contextlib.asynccontextmanager
async def async_timeout_(delay):
async with async_timeout.timeout(delay):
yield
import sys
if sys.version_info >= (3, 11):
asyncio.run(main("native", native))
asyncio.run(main("quattro", quattro_))
asyncio.run(main("anyio", anyio_))
asyncio.run(main("async_timeout", async_timeout_))
import uvloop
uvloop.install()
asyncio.run(main("quattro+uvloop", quattro_))
if sys.version_info >= (3, 11):
asyncio.run(main("native+uvloop", native))
# note='quattro' FAILED: expected a timeout!
# note='quattro' PASS: great a timeout is here
# note='anyio' PASS: great a timeout is here
# note='anyio' PASS: great a timeout is here
# note='async_timeout' PASS: great a timeout is here
# note='async_timeout' PASS: great a timeout is here
# note='quattro+uvloop' PASS: great a timeout is here
# note='quattro+uvloop' PASS: great a timeout is here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment