Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
perf(cli): parallelize session stop --all for faster cleanup
ShawnPana7769fbec94d5
diff --git a/browser_use/skill_cli/commands/cloud_task.py b/browser_use/skill_cli/commands/cloud_task.pyindex 8cc7515a9..7d7b75fca 100644--- a/browser_use/skill_cli/commands/cloud_task.py+++ b/browser_use/skill_cli/commands/cloud_task.py@@ -145,6 +145,40 @@ async def stop_session(session_id: str) -> dict[str, Any]:return result['data']+async def stop_sessions_parallel(session_ids: list[str]) -> tuple[list[str], list[dict]]:+ """Stop multiple cloud sessions in parallel using thread pool.++ Args:+ session_ids: List of session IDs to stop++ Returns:+ Tuple of (stopped_ids, errors) where errors is list of {id, error} dicts+ """++ def _stop_one_sync(session_id: str) -> tuple[str, str | None]:+ """Stop one session synchronously, return (id, error_or_none)."""+ try:+ result = _api_request('PATCH', f'/sessions/{session_id}', {'action': 'stop'})+ if not result['success']:+ return (session_id, result['error'])+ return (session_id, None)+ except Exception as e:+ return (session_id, str(e))++ # Run all stops in parallel using thread pool+ results = await asyncio.gather(*[asyncio.to_thread(_stop_one_sync, sid) for sid in session_ids])++ stopped = []+ errors = []+ for session_id, error in results:+ if error is None:+ stopped.append(session_id)+ else:+ errors.append({'id': session_id, 'error': error})++ return stopped, errors++async def delete_session(session_id: str) -> dict[str, Any]:"""Delete a cloud session and all its tasks.diff --git a/browser_use/skill_cli/commands/session_management.py b/browser_use/skill_cli/commands/session_management.pyindex 3fa505277..56fcc04de 100644--- a/browser_use/skill_cli/commands/session_management.py+++ b/browser_use/skill_cli/commands/session_management.py@@ -235,18 +235,15 @@ def _handle_stop_all(args: argparse.Namespace) -> int:print('No active sessions to stop')return 0- stopped = []- errors = []-- for s in sessions:- session_id = s.get('id')- if not session_id:- continue- try:- _run_async(cloud_task.stop_session(session_id))- stopped.append(session_id)- except Exception as e:- errors.append({'id': session_id, 'error': str(e)})+ # Extract session IDs+ session_ids = [s.get('id') for s in sessions if s.get('id')]++ if not session_ids:+ print('No active sessions to stop')+ return 0++ # Stop all sessions in parallel+ stopped, errors = _run_async(cloud_task.stop_sessions_parallel(session_ids))if getattr(args, 'json', False):print(json.dumps({'stopped': stopped, 'errors': errors}))