File tree Expand file tree Collapse file tree 1 file changed +17
-1
lines changed Expand file tree Collapse file tree 1 file changed +17
-1
lines changed Original file line number Diff line number Diff line change 1
1
import asyncio
2
-
2
+ import functools
3
+ import inspect
3
4
from typing import Awaitable
4
5
5
6
from ._utils import _ThreadedLoopAdapter
@@ -24,3 +25,18 @@ async def parse(self, response):
24
25
```
25
26
"""
26
27
return _ThreadedLoopAdapter ._ensure_future (coro )
28
+
29
+
30
+ def use_threaded_loop (callback ):
31
+ if not (inspect .iscoroutinefunction (callback ) or inspect .isasyncgenfunction (callback )):
32
+ raise RuntimeError (
33
+ f"Cannot decorate callback '{ callback .__name__ } ' with 'use_threaded_loop':"
34
+ " callback must be a coroutine function or an async generator"
35
+ )
36
+
37
+ @functools .wraps (callback )
38
+ async def wrapper (* args , ** kwargs ):
39
+ future : asyncio .Future = _ThreadedLoopAdapter ._ensure_future (callback (* args , ** kwargs ))
40
+ return await future
41
+
42
+ return wrapper
You can’t perform that action at this time.
0 commit comments