Skip to content

Commit ef3e68d

Browse files
fix(playwright): address review comments on dependencies and lazy cache
Removed browserforge and playwright from core dependencies in pyproject.toml as they belong in optional dependencies. Refactored Playwright signature cache in _playwright_browser_controller.py to load lazily via lru_cache rather than at module import time, preventing overhead when Playwright is not used.
1 parent 57da910 commit ef3e68d

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

pyproject.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,10 @@ keywords = [
3535
]
3636
dependencies = [
3737
"async-timeout>=5.0.1",
38-
"browserforge>=1.2.4",
3938
"cachetools>=5.5.0",
4039
"colorama>=0.4.0",
4140
"impit>=0.8.0",
4241
"more-itertools>=10.2.0",
43-
"playwright>=1.58.0",
4442
"protego>=0.5.0",
4543
"psutil>=6.0.0",
4644
"pydantic-settings>=2.12.0",

src/crawlee/browsers/_playwright_browser_controller.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import inspect
66
from asyncio import Lock
77
from datetime import datetime, timedelta, timezone
8+
from functools import lru_cache
89
from typing import TYPE_CHECKING, Any, cast
910

1011
from browserforge.injectors.playwright import AsyncNewContext
@@ -29,13 +30,17 @@
2930

3031
logger = getLogger(__name__)
3132

32-
# Cache Playwright signatures to avoid overhead in critical path
33-
_launch_persistent_context_params = set(inspect.signature(PlaywrightBrowserType.launch_persistent_context).parameters)
34-
_new_context_params = set(inspect.signature(Browser.new_context).parameters)
3533

36-
_common_context_options = _launch_persistent_context_params & _new_context_params
37-
_persistent_unique_context_options = _launch_persistent_context_params - _new_context_params
38-
_incognito_unique_context_options = _new_context_params - _launch_persistent_context_params
34+
# Cache Playwright signatures to avoid overhead in critical path
35+
@lru_cache(maxsize=1)
36+
def _get_context_params_cache() -> dict[str, set[str]]:
37+
launch_persistent_params = set(inspect.signature(PlaywrightBrowserType.launch_persistent_context).parameters)
38+
new_context_params = set(inspect.signature(Browser.new_context).parameters)
39+
return {
40+
'common': launch_persistent_params & new_context_params,
41+
'persistent_unique': launch_persistent_params - new_context_params,
42+
'incognito_unique': new_context_params - launch_persistent_params,
43+
}
3944

4045

4146
@docs_group('Browser management')
@@ -233,23 +238,25 @@ async def _create_browser_context(
233238
"""
234239
browser_new_context_options = dict(browser_new_context_options) if browser_new_context_options else {}
235240

241+
params_cache = _get_context_params_cache()
242+
236243
filtered_options = {}
237244
for key, value in browser_new_context_options.items():
238245
if self._use_incognito_pages:
239246
# Incognito mode (new_context)
240-
if key in _common_context_options or key in _incognito_unique_context_options:
247+
if key in params_cache['common'] or key in params_cache['incognito_unique']:
241248
filtered_options[key] = value
242-
elif key in _persistent_unique_context_options:
249+
elif key in params_cache['persistent_unique']:
243250
logger.warning(
244251
f'Option "{key}" is only supported in persistent context mode '
245252
'(use_incognito_pages=False) and will be ignored.'
246253
)
247254
else:
248255
raise TypeError(f'"{key}" is not a valid Playwright context option.')
249-
elif key in _common_context_options or key in _persistent_unique_context_options:
256+
elif key in params_cache['common'] or key in params_cache['persistent_unique']:
250257
# Persistent mode (launch_persistent_context)
251258
filtered_options[key] = value
252-
elif key in _incognito_unique_context_options:
259+
elif key in params_cache['incognito_unique']:
253260
logger.warning(
254261
f'Option "{key}" is only supported in incognito context mode '
255262
'(use_incognito_pages=True) and will be ignored.'

0 commit comments

Comments
 (0)