Last active
July 19, 2019 22:59
-
-
Save kgriffs/74b1ccfdbe572aba36d1f699a015e822 to your computer and use it in GitHub Desktop.
Convert a non-blocking synchronous function to an async coroutine function
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 functools | |
def non_blocking_sync_to_async(func): | |
"""Convert a non-blocking synchronous function to an async coroutine. | |
Warning: | |
This should only be used to wrap non-blocking synchronous functions. | |
If the function may block for an extended period of time (e.g., it | |
performs I/O), it should be scheduled to run in an Executor | |
instead. | |
""" | |
@functools.wraps(func) | |
async def wrapper(*args, **kwargs): | |
func(*args, **kwargs) | |
return wrapper |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment