File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change
1
+ import asyncio
2
+
3
+ def awaitme (f ):
4
+ """
5
+ A decorator to handle both synchronous and asynchronous functions seamlessly.
6
+
7
+ If the decorated function is asynchronous, it awaits the function's result.
8
+ If the decorated function is synchronous, it directly returns the result.
9
+
10
+ Args:
11
+ f (function): The function to be decorated. It can be either synchronous or asynchronous.
12
+
13
+ Returns:
14
+ function: An asynchronous wrapper that handles both sync and async functions gracefully.
15
+ """
16
+ async def wrapper (* args , ** kwargs ):
17
+ """
18
+ Wrapper function that calls the original function and checks whether it's a coroutine.
19
+
20
+ If the result is a coroutine, it awaits the result. Otherwise, it returns the result directly.
21
+
22
+ Args:
23
+ *args: Positional arguments to pass to the original function.
24
+ **kwargs: Keyword arguments to pass to the original function.
25
+
26
+ Returns:
27
+ Any: The result of the original function, either awaited (if async) or direct (if sync).
28
+ """
29
+ fn = f (* args , ** kwargs )
30
+ if asyncio .iscoroutine (fn ):
31
+ return await fn
32
+ return fn
33
+
34
+ return wrapper
You can’t perform that action at this time.
0 commit comments