File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change 1+ import asyncio
2+
3+ def awaitme (f ):
4+ """
5+ Decorator to handle synchronous and asynchronous functions.
6+
7+ This decorator allows a function to work seamlessly with both
8+ synchronous and asynchronous code. If the function `f` is a coroutine
9+ (asynchronous), it will await the result. Otherwise, it will treat
10+ it as a regular synchronous function and return the result directly.
11+
12+ :param f: The function to be decorated, which could be either synchronous or asynchronous.
13+ :type f: function
14+ :return: A wrapper function that handles the asynchronous behavior.
15+ :rtype: function
16+
17+ :Example:
18+
19+ #>>> @awaitme
20+ #>>> def sync_func():
21+ #>>> return 42
22+ #>>> sync_func()
23+ 42
24+
25+ #>>> @awaitme
26+ #>>> async def async_func():
27+ #>>> return 42
28+ #>>> await async_func()
29+ 42
30+ """
31+ async def _awaitme (* args , ** kwargs ):
32+ func = f (* args , ** kwargs )
33+ if asyncio .iscoroutine (func ):
34+ return await func
35+ return func
36+
37+ return _awaitme
You can’t perform that action at this time.
0 commit comments