Skip to content

Instantly share code, notes, and snippets.

@thehappycheese
Created January 24, 2023 08:11
Show Gist options
  • Save thehappycheese/65c66c787669834f51da87aacd0cb004 to your computer and use it in GitHub Desktop.
Save thehappycheese/65c66c787669834f51da87aacd0cb004 to your computer and use it in GitHub Desktop.
A hack to turn a legacy blocking funciton into an async function
def run_in_executor(f):
"""
This is a hack to turn a legacy blocking funciton into an async function.
Thanks to balki https://stackoverflow.com/a/53719009/1782370
Example:
The following example shows how to use use a blocking
`azure.identity` credential type with `pandas.read_parquet()`.
Pandas normally requires that you use one of the limited
subset of async credential types availiable in the
`azure.identity.aio` module which is annoying because you can't
use `InteractiveBrowserCredential` or `DeviceCodeCredential` which are
really handy for testing and development.
```python
import pandas as pd
from azure.identity import DeviceCodeCredential
dcr = DeviceCodeCredential()
dcr.get_token = run_in_executor(dcr.get_token)
df = pd.read_parquet(
"abfss://[email protected]/path/file.parquet",
storage_options = {
"credential":dcr
}
)
```
"""
import functools
import asyncio
@functools.wraps(f)
def inner(*args, **kwargs):
loop = asyncio.get_running_loop()
return loop.run_in_executor(None, lambda: f(*args, **kwargs))
return inner
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment