Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/crawlee/crawlers/_basic/_basic_crawler.py
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ async def add_requests(
wait_for_all_requests_to_be_added_timeout=wait_for_all_requests_to_be_added_timeout,
)

async def _use_state(
async def use_state(
self,
default_value: dict[str, JsonSerializable] | None = None,
) -> dict[str, JsonSerializable]:
Expand Down Expand Up @@ -1421,7 +1421,7 @@ async def __run_task_function(self) -> None:
add_requests=result.add_requests,
push_data=result.push_data,
get_key_value_store=result.get_key_value_store,
use_state=self._use_state,
use_state=self.use_state,
log=self._logger,
)
self._context_result_map[context] = result
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/crawlers/_basic/test_basic_crawler.py
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,20 @@ async def handler(context: BasicCrawlingContext) -> None:
assert value == {'hello': 'world'}


async def test_crawler_use_state() -> None:
crawler = BasicCrawler()

await crawler.use_state({'hello': 'world'})

@crawler.router.default_handler
async def handler(context: BasicCrawlingContext) -> None:
# The state set by the crawler must be available in the context of the request handler
state = await context.use_state()
assert state == {'hello': 'world'}

await crawler.run(['https://hello.world'])


async def test_context_use_state_crawlers_share_state() -> None:
async def handler(context: BasicCrawlingContext) -> None:
state = await context.use_state({'urls': []})
Expand Down
Loading