Skip to content

Commit 9690976

Browse files
committed
utils module
1 parent 06ec9a2 commit 9690976

File tree

3 files changed

+26
-26
lines changed

3 files changed

+26
-26
lines changed

scrapy_playwright/__init__.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
11
__version__ = "0.0.40"
2-
3-
4-
from ._utils import ensure_future # noqa: F401
5-
from .handler import ScrapyPlaywrightDownloadHandler # noqa: F401
6-
from .page import PageMethod # noqa: F401

scrapy_playwright/_utils.py

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -162,24 +162,3 @@ def stop(cls, caller_id: int) -> None:
162162
asyncio.run_coroutine_threadsafe(cls._coro_queue.join(), cls._loop)
163163
cls._loop.call_soon_threadsafe(cls._loop.stop)
164164
cls._thread.join()
165-
166-
167-
def ensure_future(coro: Awaitable) -> asyncio.Future:
168-
"""Wrap a coroutine in a Future assigned to the threaded event loop.
169-
170-
On windows, Playwright runs in an event loop of its own in a separate thread.
171-
If Playwright coroutines are awaited directly, they are assigned to the main
172-
thread's event loop, resulting in: "ValueError: The future belongs to a
173-
different loop than the one specified as the loop argument"
174-
175-
Usage:
176-
```
177-
from playwright.async_api import Page
178-
from scrapy_playwright import ensure_future
179-
180-
async def parse(self, response):
181-
page: Page = response.meta["playwright_page"]
182-
await ensure_future(page.screenshot(path="example.png", full_page=True))
183-
```
184-
"""
185-
return _ThreadedLoopAdapter._ensure_future(coro)

scrapy_playwright/utils.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import asyncio
2+
3+
from typing import Awaitable
4+
5+
from ._utils import _ThreadedLoopAdapter
6+
7+
8+
def ensure_future(coro: Awaitable) -> asyncio.Future:
9+
"""Wrap a coroutine in a Future assigned to the threaded event loop.
10+
11+
On windows, Playwright runs in an event loop of its own in a separate thread.
12+
If Playwright coroutines are awaited directly, they are assigned to the main
13+
thread's event loop, resulting in: "ValueError: The future belongs to a
14+
different loop than the one specified as the loop argument"
15+
16+
Usage:
17+
```
18+
from playwright.async_api import Page
19+
from scrapy_playwright import ensure_future
20+
21+
async def parse(self, response):
22+
page: Page = response.meta["playwright_page"]
23+
await ensure_future(page.screenshot(path="example.png", full_page=True))
24+
```
25+
"""
26+
return _ThreadedLoopAdapter._ensure_future(coro)

0 commit comments

Comments
 (0)