Skip to content

Instantly share code, notes, and snippets.

@twilligon
Created January 24, 2024 06:30
Show Gist options
  • Save twilligon/15cd5e349a041807c58de33489a2d179 to your computer and use it in GitHub Desktop.
Save twilligon/15cd5e349a041807c58de33489a2d179 to your computer and use it in GitHub Desktop.
@async_agnostic decorator for running async functions at the REPL
import asyncio
def async_agnostic(corofn):
@wraps(corofn)
def wrapper(*args, **kwargs):
with suppress(RuntimeError):
loop = asyncio.get_running_loop()
if loop.is_running():
return corofn(*args, **kwargs)
return asyncio.run(corofn(*args, **kwargs))
return wrapper
@twilligon
Copy link
Author

License: CC0-1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment