Skip to content

Commit 6ff0a17

Browse files
committed
Callback decorator (WIP)
1 parent 9690976 commit 6ff0a17

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

scrapy_playwright/utils.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import asyncio
2-
2+
import functools
3+
import inspect
34
from typing import Awaitable
45

56
from ._utils import _ThreadedLoopAdapter
@@ -24,3 +25,18 @@ async def parse(self, response):
2425
```
2526
"""
2627
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

0 commit comments

Comments
 (0)