Skip to content

Instantly share code, notes, and snippets.

@kgriffs
Last active July 19, 2019 22:59
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save kgriffs/74b1ccfdbe572aba36d1f699a015e822 to your computer and use it in GitHub Desktop.
Convert a non-blocking synchronous function to an async coroutine function
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