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
Release files for py-object-pool 1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| py-object-pool-1.1.tar.gz | 12.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| py_object_pool-1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 26.5 kB
Release files / py-object-pool-1.1.tar.gz
| Download URL | py-object-pool-1.1.tar.gz |
|---|---|
| Size | 12.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fa3a41f363a50b8bf346880bd75f45d6a0391f24a2533a140ed531316782352c
|
|
BLAKE2b-256 checksum How to use checksums |
8dc3df0e600222651ddaa43a34cf8befa7d715de5835c3be0cfe3006c52b3f63
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Release files / py_object_pool-1.1-py3-none-any.whl
| Download URL | py_object_pool-1.1-py3-none-any.whl |
|---|---|
| Size | 14.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9be717f00b861bbecc45f38108a96d7251bcaba4e02b24bbcc5115ffb9d32104
|
|
BLAKE2b-256 checksum How to use checksums |
09cd6ca8bcf55c71219428b2d2734303aec6137afdf372284e79ee21686b1462
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|