Skip to content

Commit efef3e9

Browse files
Mantisusvdusek
andauthored
feat(crawlers)!: Remove use_session_pool and always use sessions (#2439)
### Description The `use_session_pool` option is removed. Every crawler now processes each request with a session from its `SessionPool`, so `context.session` is always a `Session` and handlers no longer need `if context.session` checks. A request bound by `session_id` to a session that's no longer usable still fails with `RequestCollisionError`. Its failed request handler gets that session while the pool still holds it, otherwise a retired session with the bound id. For crawlers that ran without the pool, 401, 403 and 429 responses now rotate the session, and with `proxy_urls` a session keeps its proxy. ### Issues - Related: #2139, #2298 ### Testing - Updated the tests that ran without the pool and added a unit test for the failed request handler of a request whose bound session is gone. *✍️ Drafted by Claude Code* --------- Co-authored-by: Vlada Dusek <v.dusek96@gmail.com>
1 parent 427d798 commit efef3e9

31 files changed

Lines changed: 186 additions & 207 deletions

‎docs/03_concepts/02_requests.mdx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ The crawler's `max_crawl_depth` option limits how deep the crawl goes. Requests
7272

7373
## Binding to a session
7474

75-
Pass `session_id` to `from_url` to bind a request to one session from the crawler's <ApiLink to="class/SessionPool">`SessionPool`</ApiLink>. The crawler then processes the request with that session only. If the session isn't available anymore, the request fails with a <ApiLink to="class/RequestCollisionError">`RequestCollisionError`</ApiLink> and isn't retried. For details on sessions, see [Session management](./session-management).
75+
Pass `session_id` to `from_url` to bind a request to one session from the crawler's <ApiLink to="class/SessionPool">`SessionPool`</ApiLink>. The crawler then processes the request with that session only. If the session isn't usable anymore, the request fails with a <ApiLink to="class/RequestCollisionError">`RequestCollisionError`</ApiLink> and isn't retried. In the failed request handler, `context.session` is then that session while the pool still holds it, otherwise a retired session with the bound id. For details on sessions, see [Session management](./session-management).
7676

7777
## Request lifecycle
7878

‎docs/03_concepts/03_crawling_context.mdx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ For details on the crawler-specific attributes, see [HTTP crawlers](./http-crawl
3030

3131
The `request` attribute is the <ApiLink to="class/Request">`Request`</ApiLink> being processed. It carries the URL, the label used for routing, user data and the retry count. For details, see [Requests](./requests).
3232

33-
The `session` attribute is the <ApiLink to="class/Session">`Session`</ApiLink> assigned to the request, or `None` when the crawler runs without a session pool. You can call `mark_bad()` or `retire()` on it from the handler when a page shows signs of blocking. For details, see [Session management](./session-management).
33+
The `session` attribute is the <ApiLink to="class/Session">`Session`</ApiLink> assigned to the request. You can call `mark_bad()` or `retire()` on it from the handler when a page shows signs of blocking. For details, see [Session management](./session-management).
3434

3535
The `proxy_info` attribute is a <ApiLink to="class/ProxyInfo">`ProxyInfo`</ApiLink> describing the proxy used for the request, or `None` when no proxy is configured. For details, see [Proxy management](./proxy-management).
3636

‎docs/03_concepts/15_cookie_management.mdx‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,6 @@ Access the jar through <ApiLink to="class/Session#cookies">`context.session.cook
3131
{ReadWriteCookies}
3232
</RunnableCodeBlock>
3333

34-
:::note
35-
36-
`context.session` is `None` only when the crawler runs without a session pool (<ApiLink to="class/BasicCrawler#__init__">`use_session_pool`</ApiLink> set to `False`). With the default pool it's always set. The examples guard with `if context.session is None: return` to cover that case.
37-
38-
:::
39-
4034
A cookie has a `name`, a `value`, and optional parameters such as `domain`. If a cookie has no `domain`, it applies to any domain, so a login cookie set that way is sent to every host you crawl. Set `domain` on anything sensitive. For the full set of parameters, see <ApiLink to="class/CookieParam">`CookieParam`</ApiLink>. To dump the whole jar, use <ApiLink to="class/SessionCookies#get_cookies_as_dicts">`get_cookies_as_dicts()`</ApiLink>, and to load several cookies at once, use <ApiLink to="class/SessionCookies#set_cookies">`set_cookies()`</ApiLink>.
4135

4236
## Seeding cookies on new sessions

‎docs/03_concepts/code_examples/cookie_management/playwright_cookies.py‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ async def main() -> None:
1313

1414
@crawler.router.default_handler
1515
async def handler(context: PlaywrightCrawlingContext) -> None:
16-
if context.session is None:
17-
return
18-
1916
# The browser receives the `visited` cookie during the first navigation.
2017
# It's synced back onto the session after the handler returns, so on the
2118
# second request it's already on `context.session`.

‎docs/03_concepts/code_examples/cookie_management/read_write_cookies.py‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ async def main() -> None:
88

99
@crawler.router.default_handler
1010
async def handler(context: HttpCrawlingContext) -> None:
11-
if context.session is None:
12-
return
13-
1411
# Set a cookie on the session. It's sent with every following
1512
# request that runs on this session.
1613
context.session.cookies.set(

‎docs/03_concepts/code_examples/cookie_management/retry_pinned_session.py‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ async def main() -> None:
99

1010
@crawler.router.default_handler
1111
async def handler(context: HttpCrawlingContext) -> None:
12-
if context.session is None:
13-
return
14-
1512
if context.request.user_data.get('prepared'):
1613
# Cookies from the setup step are on this pinned session.
1714
context.log.info(

‎docs/03_concepts/code_examples/cookie_management/retry_restore_cookies.py‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ async def main() -> None:
1212

1313
@crawler.router.default_handler
1414
async def handler(context: HttpCrawlingContext) -> None:
15-
if context.session is None:
16-
return
17-
1815
state = await context.use_state(default_value={})
1916
if not state.get('cookies'):
2017
# First pass: establish cookies once and store them for every session,
@@ -30,7 +27,7 @@ async def apply_cookies(context: BasicCrawlingContext) -> None:
3027
# Runs before navigation. Apply the shared cookies to whichever session
3128
# handles the request, so every session ends up with them.
3229
state = await context.use_state(default_value={})
33-
if context.session and (cookies := state.get('cookies')):
30+
if cookies := state.get('cookies'):
3431
context.session.cookies.set_cookies(cast('list[CookieParam]', cookies))
3532

3633
await crawler.run(['https://httpbingo.org/cookies'])

‎docs/03_concepts/code_examples/cookie_management/retry_single_session.py‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ async def main() -> None:
2323

2424
@crawler.router.default_handler
2525
async def handler(context: HttpCrawlingContext) -> None:
26-
if context.session is None:
27-
return
28-
2926
# First attempt: establish cookies, then raise to trigger a retry.
3027
if not context.session.cookies:
3128
await context.send_request('https://httpbingo.org/cookies/set?logged_in=1')

‎docs/03_concepts/code_examples/proxy_management/session_bs_example.py‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ async def main() -> None:
1212
'http://proxy-2.com/',
1313
]
1414
)
15-
crawler = BeautifulSoupCrawler(
16-
proxy_configuration=proxy_configuration,
17-
use_session_pool=True,
18-
)
15+
crawler = BeautifulSoupCrawler(proxy_configuration=proxy_configuration)
1916

2017
# ...
2118

‎docs/03_concepts/code_examples/proxy_management/session_pw_example.py‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ async def main() -> None:
1212
'http://proxy-2.com/',
1313
]
1414
)
15-
crawler = PlaywrightCrawler(
16-
proxy_configuration=proxy_configuration,
17-
use_session_pool=True,
18-
)
15+
crawler = PlaywrightCrawler(proxy_configuration=proxy_configuration)
1916

2017
# ...
2118

0 commit comments

Comments
 (0)