Last active
July 19, 2019 22:59
Star
You must be signed in to star a gist
Convert a non-blocking synchronous function to an async coroutine function
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 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