Manage a pool of browsers and pages, handling their lifecycle and resource allocation. The `BrowserPool` is responsible for opening and closing browsers, managing pages within those browsers, and handling the overall lifecycle of these resources. It provides flexible configuration via c
| 32 | |
| 33 | @docs_group('Browser management') |
| 34 | class BrowserPool: |
| 35 | """Manage a pool of browsers and pages, handling their lifecycle and resource allocation. |
| 36 | |
| 37 | The `BrowserPool` is responsible for opening and closing browsers, managing pages within those browsers, |
| 38 | and handling the overall lifecycle of these resources. It provides flexible configuration via |
| 39 | constructor options, which include various hooks that allow for the insertion of custom behavior |
| 40 | at different stages of the browser and page lifecycles. |
| 41 | |
| 42 | The browsers in the pool can be in one of three states: active, inactive, or closed. |
| 43 | """ |
| 44 | |
| 45 | _GENERATED_PAGE_ID_LENGTH = 8 |
| 46 | """The length of the newly generated page ID.""" |
| 47 | |
| 48 | def __init__( |
| 49 | self, |
| 50 | plugins: Sequence[BrowserPlugin] | None = None, |
| 51 | *, |
| 52 | operation_timeout: timedelta = timedelta(seconds=60), |
| 53 | browser_inactive_threshold: timedelta = timedelta(seconds=10), |
| 54 | identify_inactive_browsers_interval: timedelta = timedelta(seconds=20), |
| 55 | close_inactive_browsers_interval: timedelta = timedelta(seconds=30), |
| 56 | retire_browser_after_page_count: int = 100, |
| 57 | ) -> None: |
| 58 | """Initialize a new instance. |
| 59 | |
| 60 | Args: |
| 61 | plugins: Browser plugins serve as wrappers around various browser automation libraries, |
| 62 | providing a consistent interface across different libraries. |
| 63 | operation_timeout: Operations of the underlying automation libraries, such as launching a browser |
| 64 | or opening a new page, can sometimes get stuck. To prevent `BrowserPool` from becoming unresponsive, |
| 65 | we add a timeout to these operations. |
| 66 | browser_inactive_threshold: The period of inactivity after which a browser is considered as inactive. |
| 67 | identify_inactive_browsers_interval: The period of inactivity after which a browser is considered |
| 68 | as retired. |
| 69 | close_inactive_browsers_interval: The interval at which the pool checks for inactive browsers |
| 70 | and closes them. The browser is considered as inactive if it has no active pages and has been idle |
| 71 | for the specified period. The browser is considered as retired if it has no active pages and has total |
| 72 | pages count greater than or equal to `retire_browser_after_page_count`. |
| 73 | retire_browser_after_page_count: The maximum number of processed pages after which the browser is considered |
| 74 | as retired. |
| 75 | """ |
| 76 | self._plugins = plugins or [PlaywrightBrowserPlugin()] |
| 77 | self._operation_timeout = operation_timeout |
| 78 | self._browser_inactive_threshold = browser_inactive_threshold |
| 79 | |
| 80 | self._active_browsers = list[BrowserController]() |
| 81 | """A list of browsers currently active and being used to open pages.""" |
| 82 | |
| 83 | self._inactive_browsers = list[BrowserController]() |
| 84 | """A list of browsers currently inactive and not being used to open new pages, |
| 85 | but may still contain open pages.""" |
| 86 | |
| 87 | self._identify_inactive_browsers_task = RecurringTask( |
| 88 | self._identify_inactive_browsers, |
| 89 | identify_inactive_browsers_interval, |
| 90 | ) |
| 91 |
no outgoing calls