Repository navigation
feat(autoscaling)!: Add ConcurrencySystem to share one concurrency budget between crawlers
#2322
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 all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fe99076
add `ConcurrencySystem` to share one concurrency budget between crawlers
Mantisus a258ba2
Update src/crawlee/autoscaling/_types.py
Mantisus 8080cbf
address review comments
Mantisus 273954b
update upgrading guide and tests
Mantisus 98ff12f
add test `test_failure_awaits_running_tasks`
Mantisus 31e6c82
use the crawler id as the consumer id
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
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
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
48 changes: 48 additions & 0 deletions
48
docs/guides/code_examples/scaling_crawlers/shared_concurrency_system_example.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,48 @@ | ||
| import asyncio | ||
|
|
||
| from crawlee import ConcurrencySettings | ||
| from crawlee.autoscaling import ConcurrencySystem | ||
| from crawlee.crawlers import ( | ||
| BeautifulSoupCrawler, | ||
| BeautifulSoupCrawlingContext, | ||
| ParselCrawler, | ||
| ParselCrawlingContext, | ||
| ) | ||
| from crawlee.storages import RequestQueue | ||
|
|
||
|
|
||
| async def main() -> None: | ||
| concurrency_settings = ConcurrencySettings(max_concurrency=10) | ||
|
|
||
| # Each crawler needs a request queue of its own, otherwise they would both | ||
| # consume the default one and process each other's requests. | ||
| first_queue = await RequestQueue.open(alias='first-crawler') | ||
| second_queue = await RequestQueue.open(alias='second-crawler') | ||
|
|
||
| # One budget for both crawlers, instead of one each. | ||
| async with ConcurrencySystem(concurrency_settings) as concurrency_system: | ||
| first_crawler = BeautifulSoupCrawler( | ||
| concurrency_system=concurrency_system, | ||
| request_manager=first_queue, | ||
| ) | ||
| second_crawler = ParselCrawler( | ||
| concurrency_system=concurrency_system, | ||
| request_manager=second_queue, | ||
| ) | ||
|
|
||
| @first_crawler.router.default_handler | ||
| async def first_handler(context: BeautifulSoupCrawlingContext) -> None: | ||
| context.log.info(f'Processing {context.request.url}...') | ||
|
|
||
| @second_crawler.router.default_handler | ||
| async def second_handler(context: ParselCrawlingContext) -> None: | ||
| context.log.info(f'Processing {context.request.url}...') | ||
|
|
||
| await asyncio.gather( | ||
| first_crawler.run(['https://crawlee.dev/']), | ||
| second_crawler.run(['https://crawlee.dev/python/']), | ||
| ) | ||
|
|
||
|
|
||
| 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
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 was deleted.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| from ._autoscaled_pool import AutoscaledPool | ||
| from ._concurrency_system import ConcurrencySystem | ||
| from ._types import ConcurrencyConsumer, LoadRatioInfo, SystemInfo | ||
|
|
||
| __all__ = [ | ||
| 'AutoscaledPool', | ||
| 'ConcurrencyConsumer', | ||
| 'ConcurrencySystem', | ||
| 'LoadRatioInfo', | ||
| 'SystemInfo', | ||
| ] |
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.