@@ -53,6 +53,7 @@ def __init__(
53
53
browser_inactive_threshold : timedelta = timedelta (seconds = 10 ),
54
54
identify_inactive_browsers_interval : timedelta = timedelta (seconds = 20 ),
55
55
close_inactive_browsers_interval : timedelta = timedelta (seconds = 30 ),
56
+ retire_browser_after_page_count : int = 100 ,
56
57
) -> None :
57
58
"""Initialize a new instance.
58
59
@@ -67,7 +68,10 @@ def __init__(
67
68
as retired.
68
69
close_inactive_browsers_interval: The interval at which the pool checks for inactive browsers
69
70
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.
71
75
"""
72
76
self ._plugins = plugins or [PlaywrightBrowserPlugin ()]
73
77
self ._operation_timeout = operation_timeout
@@ -91,6 +95,7 @@ def __init__(
91
95
)
92
96
93
97
self ._total_pages_count = 0
98
+ self ._retire_browser_after_page_count = retire_browser_after_page_count
94
99
self ._pages = WeakValueDictionary [str , CrawleePage ]() # Track the pages in the pool
95
100
self ._plugins_cycle = itertools .cycle (self ._plugins ) # Cycle through the plugins
96
101
@@ -305,6 +310,9 @@ async def _get_new_page(
305
310
except RuntimeError as exc :
306
311
raise RuntimeError ('Browser pool is not initialized.' ) from exc
307
312
313
+ if browser_controller .total_opened_pages >= self ._retire_browser_after_page_count :
314
+ self ._retire_browser (browser_controller )
315
+
308
316
crawlee_page = CrawleePage (id = page_id , page = page , browser_type = plugin .browser_type )
309
317
self ._pages [page_id ] = crawlee_page
310
318
self ._total_pages_count += 1
@@ -321,6 +329,12 @@ def _pick_browser_with_free_capacity(
321
329
322
330
return None
323
331
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
+
324
338
async def _launch_new_browser (self , plugin : BrowserPlugin ) -> BrowserController :
325
339
"""Launch a new browser instance using the specified plugin."""
326
340
browser = await plugin .new_browser ()
0 commit comments