Skip to main content

Handle multiple playwright pages concurrently with ease and speed.

Project description

Playwright Page Pool 🎭🏊‍♀️

PyPI version License: MIT

Welcome to Playwright Page Pool! 🎉

This Python module provides a convenient way to manage a pool of Playwright browser pages within a given browser context. By leveraging Python's async capabilities, you can handle multiple pages concurrently with ease and speed, ensuring your tasks are executed swiftly and efficiently. 🚀 It allows you to efficiently reuse pages or create new pages on demand, making your web automation tasks a breeze! ✨

Features 🌟

  • 🌐 The best tool for working across multiple pages
  • 🏊‍♀️ Manages a pool of Playwright browser pages
  • 🔄 Supports reuse of pages for improved performance
  • 🆕 Creates new pages on demand when needed
  • 🚀 Utilizes asynchronous operations for efficient execution
  • 🔒 Ensures thread safety with semaphores and events
  • 🎨 Customizable page initialization with page_initiator callback
  • 🎯 Automatic closing of pages when the pool is no longer needed

Installation 🔧

To start using Playwright Page Pool, simply install it via pip:

pip install playwright-page-pool

Make sure you have Playwright installed and set up in your project as well.

Usage 📝

Here's a quick example of how to use Playwright Page Pool in your code:

import asyncio

from playwright.async_api import async_playwright
from playwright_page_pool import PagePool

async def run_example(pool: PagePool) -> None:
    async with pool.acquire() as page:
        await page.goto("https://example.com")
        print(await page.title())

async def main(*args, **kwargs) -> None:
    async with async_playwright() as p:
        browser = await p.chromium.launch()
        context = await browser.new_context()

        async with PagePool(context=context, reuse_pages=True) as pool:
            run_examples = [run_example(pool) for _ in range(10)]
            await asyncio.gather(*run_examples)  # 10 tasks are executed concurrently

        await browser.close()


if __name__ == "__main__":
    asyncio.run(main())

In this example, we create a PagePool instance with a Playwright browser context. We set reuse_pages=True to enable page reuse for improved performance. We then define a run_example function that acquires a page from the pool using the acquire context manager, navigates to a URL, and prints the page title. Finally, we create 10 tasks using run_example and execute them concurrently using asyncio.gather. The PagePool manages the allocation and reuse of pages efficiently.

Configuration 🛠️

When creating a PagePool instance, you can customize its behavior with the following parameters:

  • context: The Playwright browser context associated with the page pool.
  • max_pages: The maximum number of pages that can be opened at once. Defaults to the number of CPU cores.
  • page_initiator: An optional asynchronous callable that is called each time a new page is created. This allows you to perform custom initialization on each page.
  • reuse_pages: Determines whether pages should be reused. If False, a new page is created for each acquisition. Defaults to False. When set to True, it eliminates the overhead of opening and closing pages repeatedly, thereby improving performance.

Contributing 🤝

Contributions to Playwright Page Pool are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request on the GitHub repository.

License 📄

This project is licensed under the MIT License. See the LICENSE file for more details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

playwright_page_pool-0.0.1.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

playwright_page_pool-0.0.1-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file playwright_page_pool-0.0.1.tar.gz.

File metadata

  • Download URL: playwright_page_pool-0.0.1.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.8.19 Darwin/23.4.0

File hashes

Hashes for playwright_page_pool-0.0.1.tar.gz
Algorithm Hash digest
SHA256 810f6870fe6f88697737eacd864f00d0121bebce817c7d3f796686ddbea1fdb8
MD5 1bc8b0f611043cf88619bfb4b5eb056d
BLAKE2b-256 b5dd9e3e2eebc64c2b999ebc5bc49fc774d1d030a154c6fe26fa173fd0ba13bf

See more details on using hashes here.

File details

Details for the file playwright_page_pool-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for playwright_page_pool-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fc27fb189ba4c3fde1a982b609c40ee0bae3c0604224342c26d8caa351070034
MD5 1be5bc7614c577b6bbf242b2219567d7
BLAKE2b-256 f099e5817a5f9574d833c9d7d7370e4757670687366f0fb7017833b423532caa

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page