Last active
July 28, 2024 23:43
-
-
Save mezhgano/5b311f33a52652628c419d9532dc17fd to your computer and use it in GitHub Desktop.
Playwright persistent context concurrency
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
import asyncio | |
import random | |
from pathlib import Path | |
from playwright.async_api import BrowserContext, async_playwright | |
CWD = Path().resolve() | |
# Example path | |
user_data_dir = Path.joinpath(CWD, 'playwright/data_dir/chromium') | |
user_data_dir.mkdir(parents=True, exist_ok=True) | |
async def open_page(url: str, browser: BrowserContext, sleep: int): | |
'Open page in given `BrowserContext`' | |
page = await browser.new_page() | |
await page.goto(url, wait_until='domcontentloaded') | |
# Simulate some job | |
await asyncio.sleep(sleep) | |
await page.close() | |
async def main(url: str, tabs: int) -> None: | |
'Open multiple `tabs` asynchronous, in same browser.' | |
semaphore = asyncio.BoundedSemaphore(5) | |
async with semaphore, async_playwright() as playwright: | |
browser = await playwright.chromium.launch_persistent_context( | |
user_data_dir=user_data_dir, | |
headless = False, | |
) | |
tasks = [] | |
for _ in range(tabs): | |
tasks.append( | |
asyncio.create_task( | |
open_page( | |
url=url, | |
browser=browser, | |
sleep=random.randint(3, 10) | |
))) | |
await asyncio.wait(tasks) | |
if __name__ == '__main__': | |
url = 'https://example.com/' | |
tabs = 5 | |
asyncio.run(main(url, tabs)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment