From f41b8aabd9a3e23004efaf5e615ad15c8128f00f Mon Sep 17 00:00:00 2001 From: Robert Date: Tue, 19 Mar 2024 00:24:45 +0700 Subject: [PATCH] sync to async --- chain_service/utils/__init__.py | 0 chain_service/utils/sync_to_async.py | 12 ++++++++++++ 2 files changed, 12 insertions(+) create mode 100644 chain_service/utils/__init__.py create mode 100644 chain_service/utils/sync_to_async.py diff --git a/chain_service/utils/__init__.py b/chain_service/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/chain_service/utils/sync_to_async.py b/chain_service/utils/sync_to_async.py new file mode 100644 index 0000000..ec07765 --- /dev/null +++ b/chain_service/utils/sync_to_async.py @@ -0,0 +1,12 @@ +import asyncio +from functools import wraps +from typing import Callable, Tuple, Dict + + +def sync_to_async(function: Callable): + + @wraps(function) + async def wrapper(*args: Tuple, **kwargs: Dict): + return await asyncio.to_thread(function, *args, **kwargs) + + return wrapper