Table of contents
Object pool library creates a pool of resource class instance and use them in your project. Pool is implemented using python built in library Queue.
Let's say for example, you need multiple firefox browser object in headless mode to be available for client request to process or some testing or scraping.
- Each time creating a new browser instance is time consuming task which will make client to wait.
- If you have one browser instance and manage with browser tab, it will become cumbersome to maintain and debug in case of any issue arises.
Object Pool will help you to manage in that situation as it creates resource pool and provides to each client when it requests. Thus separating the process from one another without waiting or creating new instance on the spot.
How to install
pip install py-object-pool
Requirements
Python 3.6 and above
Below example will help you to understand how to create resource class for ObjectPool and use them in your project.
In the below example, Browser is a resource class and ff_browser_pool is a pool of Browser.
Please refer the user guide for more details.
from selenium.webdriver import Firefox, FirefoxProfile
from selenium.webdriver.firefox.options import Options
class FirefoxBrowser:
"""
This is browser resource class for ObjectPool. This class demonstrate how to create resource class
and implement methods described in the user guide section.
"""
def __init__(self):
self.browser = FirefoxBrowser.create_ff_browser()
self.page_title = None
@classmethod
def create_ff_browser(cls):
"""Returns headless firefox browser object"""
profile = FirefoxProfile().set_preference("intl.accept_languages", "es")
opts = Options()
opts.headless = True
browser = Firefox(options=opts, firefox_profile=profile)
return browser
def get_page_title(self, url):
"""Returns page title of the url"""
browser = self.browser
browser.get(url)
self.page_title = browser.title
return self.page_title
def clean_up(self, **stats):
"""quits browser and sets None, when this method is called"""
self.browser.quit()
self.browser = None
def check_invalid(self, **stats):
"""Returns invalid=True if the browser accessed google web page, otherwise False"""
if self.page_title == 'Google':
return True
return False
from object_pool import ObjectPool
ff_browser_pool = ObjectPool(FirefoxBrowser, min_init=2)
with ff_browser_pool.get() as (browser, browser_stats):
title = browser.get_page_title('https://www.google.co.in/')
Durai Pandian - Initial work - dduraipandian
This project is licensed under the MIT License - see the LICENSE file for details