@@ -53,6 +53,7 @@ def __init__(
5353 browser_inactive_threshold : timedelta = timedelta (seconds = 10 ),
5454 identify_inactive_browsers_interval : timedelta = timedelta (seconds = 20 ),
5555 close_inactive_browsers_interval : timedelta = timedelta (seconds = 30 ),
56+ retire_browser_after_page_count : int = 100 ,
5657 ) -> None :
5758 """Initialize a new instance.
5859
@@ -67,7 +68,10 @@ def __init__(
6768 as retired.
6869 close_inactive_browsers_interval: The interval at which the pool checks for inactive browsers
6970 and closes them. The browser is considered as inactive if it has no active pages and has been idle
70- for the specified period.
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.
7175 """
7276 self ._plugins = plugins or [PlaywrightBrowserPlugin ()]
7377 self ._operation_timeout = operation_timeout
@@ -91,6 +95,7 @@ def __init__(
9195 )
9296
9397 self ._total_pages_count = 0
98+ self ._retire_browser_after_page_count = retire_browser_after_page_count
9499 self ._pages = WeakValueDictionary [str , CrawleePage ]() # Track the pages in the pool
95100 self ._plugins_cycle = itertools .cycle (self ._plugins ) # Cycle through the plugins
96101
@@ -305,6 +310,9 @@ async def _get_new_page(
305310 except RuntimeError as exc :
306311 raise RuntimeError ('Browser pool is not initialized.' ) from exc
307312
313+ if browser_controller .total_opened_pages >= self ._retire_browser_after_page_count :
314+ self ._retire_browser (browser_controller )
315+
308316 crawlee_page = CrawleePage (id = page_id , page = page , browser_type = plugin .browser_type )
309317 self ._pages [page_id ] = crawlee_page
310318 self ._total_pages_count += 1
@@ -321,6 +329,12 @@ def _pick_browser_with_free_capacity(
321329
322330 return None
323331
332+ def _retire_browser (self , browser : BrowserController ) -> None :
333+ """Retire a browser by moving it to the inactive list."""
334+ if browser in self ._active_browsers :
335+ self ._active_browsers .remove (browser )
336+ self ._inactive_browsers .append (browser )
337+
324338 async def _launch_new_browser (self , plugin : BrowserPlugin ) -> BrowserController :
325339 """Launch a new browser instance using the specified plugin."""
326340 browser = await plugin .new_browser ()
0 commit comments