Created
January 24, 2024 06:30
-
-
Save twilligon/15cd5e349a041807c58de33489a2d179 to your computer and use it in GitHub Desktop.
@async_agnostic decorator for running async functions at the REPL
This file contains hidden or 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 | |
| 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
License: CC0-1.0