Skip to content

Instantly share code, notes, and snippets.

@ThatXliner
Created October 7, 2020 16:41
Show Gist options
  • Save ThatXliner/86e3c4d66f7872da53cb37d738ec9282 to your computer and use it in GitHub Desktop.
Save ThatXliner/86e3c4d66f7872da53cb37d738ec9282 to your computer and use it in GitHub Desktop.
Asyncify: A hacky way of making your functions asynchronous.
#!/usr/bin/env python3
"""
Asyncify: A hacky way of making your functions asynchronous.
Author: Bryan Hu
@ThatXliner
Asyncio and asynchronous code is the modern way of coding.
But, no one wants to do a whole lot of research on asynchronous
libraries. In fact, `async def` is what makes your function
asynchronous. So, why not append `async` on all of your `def`s?
Asyncify provides a decorator that will return an `await`able.
Thus, making your APIs asynchronous with one line of
@asyncify.asyncify
or, if you did `from asyncify import asyncify`
@asyncify
DISCLAIMER: I don't think this should be taken seriously. If you
*truely* want an asynchronous API, you should rewrite the functions
you use in a asynchronous manner. For example, if you're webscraping,
you should use an asynchronous library like httpx or aiohttp.
Stacksearch (My repository) is an example of having a
"truely asynchronous" API.
"""
from typing import Callable, Awaitable
def asyncify(some_func: Callable) -> Awaitable:
"""A hacky way of making your functions asynchronous."""
async def asynchronous_wrapper(*args, **kwargs):
"""A asynchronous function that runs a synchronous function."""
some_func(*args, **kwargs)
return asynchronous_wrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment