Created
June 15, 2023 07:22
-
-
Save napsternxg/0c08c32027b059a63fc95fc577dd26bd to your computer and use it in GitHub Desktop.
Async Decorator
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 | |
def async_decorator(acreate_fn): | |
async def _f(*args, **kwargs): | |
print(f"Decorated fn: {args=}, {kwargs=}. Sleeping.") | |
await asyncio.sleep(0.1) | |
return await acreate_fn(*args, **kwargs) | |
return _f | |
@async_decorator | |
async def some_func(i): | |
print(f"Regular fn: {i=}") | |
return i | |
print(asyncio.run(some_func(10))) | |
""" | |
Decorated fn: args=(10,), kwargs={}. Sleeping. | |
Regular fn: i=10 | |
10 | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment