-
Notifications
You must be signed in to change notification settings - Fork 547
feat: Persist the SitemapRequestLoader state
#1347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 19 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
0f15326
add persist state in sitemap loader
Mantisus d7c8c9e
add test
Mantisus 91118b5
update use state
Mantisus 9f8d7b4
up tests
Mantisus f7b0dfb
up tests
Mantisus c8cf0d5
check test
Mantisus c3f0f68
use fixture
Mantisus 7ee1ab9
up docs
Mantisus fa76aca
up docs
Mantisus 34008c9
add smaap wait in abort loading
Mantisus 1c30115
up default example
Mantisus 8cfec56
up basic guide
Mantisus 0bdec01
up guide
Mantisus ed7116c
remove extra sleep
Mantisus e28dccf
remove sleep from example
Mantisus 82ae318
up example
Mantisus 812f9fa
protect _get_state
Mantisus b34ad2b
fix flaky tests
Mantisus ccc9b68
move requests from in_progress to queue after restore
Mantisus 97570f3
add comment in example
Mantisus 0600583
resolve
Mantisus 24cf32b
Merge branch 'master' into persist-sitemap-loader
Mantisus c5e0609
await link in `fetch_next_request`
Mantisus c1bfd79
add processed_sitemap_urls
Mantisus 097ab25
resolve
Mantisus 0f71af2
add comment
Mantisus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
docs/guides/code_examples/request_loaders/rl_basic_example_with_persist.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import asyncio | ||
| import logging | ||
|
|
||
| from crawlee import service_locator | ||
| from crawlee.request_loaders import RequestList | ||
|
|
||
| logging.basicConfig(level=logging.INFO, format='%(asctime)s-%(levelname)s-%(message)s') | ||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| # Disable clearing the `KeyValueStore` on each run. | ||
| # This is necessary so that the state keys are not cleared at startup. | ||
| # The recommended way to achieve this behavior is setting the environment variable | ||
| # `CRAWLEE_PURGE_ON_START=0` | ||
| configuration = service_locator.get_configuration() | ||
| configuration.purge_on_start = False | ||
|
|
||
|
|
||
| async def main() -> None: | ||
| # Open the request list, if it does not exist, it will be created. | ||
| # Leave name empty to use the default request list. | ||
| request_list = RequestList( | ||
| name='my-request-list', | ||
| requests=[ | ||
| 'https://apify.com/', | ||
| 'https://crawlee.dev/', | ||
| 'https://crawlee.dev/python/', | ||
| ], | ||
| # Enable persistence | ||
| persist_state_key='my-persist-state', | ||
| persist_requests_key='my-persist-requests', | ||
| ) | ||
|
|
||
| # We receive only one request. | ||
| # Each time you run it, it will be a new request until you exhaust the `RequestList`. | ||
| request = await request_list.fetch_next_request() | ||
| if request: | ||
| logger.info(f'Processing request: {request.url}') | ||
| # Do something with it... | ||
|
|
||
| # And mark it as handled. | ||
| await request_list.mark_request_as_handled(request) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| asyncio.run(main()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
docs/guides/code_examples/request_loaders/sitemap_example_with_persist.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import asyncio | ||
| import logging | ||
|
|
||
| from crawlee import service_locator | ||
| from crawlee.http_clients import ImpitHttpClient | ||
| from crawlee.request_loaders import SitemapRequestLoader | ||
|
|
||
| logging.basicConfig(level=logging.INFO, format='%(asctime)s-%(levelname)s-%(message)s') | ||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| # Disable clearing the `KeyValueStore` on each run. | ||
| # This is necessary so that the state keys are not cleared at startup. | ||
| # The recommended way to achieve this behavior is setting the environment variable | ||
| # `CRAWLEE_PURGE_ON_START=0` | ||
| configuration = service_locator.get_configuration() | ||
| configuration.purge_on_start = False | ||
|
|
||
|
|
||
| async def main() -> None: | ||
| # Create an HTTP client for fetching sitemaps | ||
| # Use the context manager for `SitemapRequestLoader` to correctly save the state when | ||
| # the work is completed. | ||
| async with ( | ||
| ImpitHttpClient() as http_client, | ||
| SitemapRequestLoader( | ||
| sitemap_urls=['https://crawlee.dev/sitemap.xml'], | ||
| http_client=http_client, | ||
| # Enable persistence | ||
| persist_state_key='my-persist-state', | ||
| ) as sitemap_loader, | ||
| ): | ||
| # Wait for the sitemap loader to fetch and parse the sitemap URLs. | ||
| # Sitemap loading happens asynchronously in the background, so we need to wait | ||
| # until at least one URL becomes available for processing. | ||
| while await sitemap_loader.is_empty() and not await sitemap_loader.is_finished(): | ||
| logger.info('Sitemap is still being loaded and parsed, waiting for URLs...') | ||
| await asyncio.sleep(0.2) | ||
|
|
||
| # We receive only one request. | ||
| # Each time you run it, it will be a new request until you exhaust the sitemap. | ||
| request = await sitemap_loader.fetch_next_request() | ||
| if request: | ||
| logger.info(f'Processing request: {request.url}') | ||
| # Do something with it... | ||
|
|
||
| # And mark it as handled. | ||
| await sitemap_loader.mark_request_as_handled(request) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| asyncio.run(main()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.