Object pool creation library
Project description
ObjectPool
Object Pool
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.
Code Example
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.
Sample resource class
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
Sample client block
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/')
License
This project is licensed under the MIT License - see the LICENSE file for details
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file py-object-pool-1.1.tar.gz
.
File metadata
- Download URL: py-object-pool-1.1.tar.gz
- Upload date:
- Size: 12.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.6.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fa3a41f363a50b8bf346880bd75f45d6a0391f24a2533a140ed531316782352c |
|
MD5 | dc0b11b2afec01b72fd8311b2c8d2c05 |
|
BLAKE2b-256 | 8dc3df0e600222651ddaa43a34cf8befa7d715de5835c3be0cfe3006c52b3f63 |
File details
Details for the file py_object_pool-1.1-py3-none-any.whl
.
File metadata
- Download URL: py_object_pool-1.1-py3-none-any.whl
- Upload date:
- Size: 14.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.6.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9be717f00b861bbecc45f38108a96d7251bcaba4e02b24bbcc5115ffb9d32104 |
|
MD5 | 0350fb304886d3b8127c2de5cfdf924c |
|
BLAKE2b-256 | 09cd6ca8bcf55c71219428b2d2734303aec6137afdf372284e79ee21686b1462 |