Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

max_workers added #129

Merged
merged 9 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pynautobot/core/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class Api(object):
:param str token: Your Nautobot token.
:param bool,optional threading: Set to True to use threading in ``.all()``
and ``.filter()`` requests.
:param int,optional max_workers: Set the maximum workers for threading in ``.all()``
and ``.filter()`` requests.
:param str,optional api_version: Set to override the default Nautobot REST API Version
for all requests.
:param int,optional retries: Number of retries, for HTTP codes 429, 500, 502, 503, 504,
Expand All @@ -74,6 +76,7 @@ def __init__(
url,
token=None,
threading=False,
max_workers=4,
api_version=None,
retries=0,
):
Expand All @@ -94,6 +97,7 @@ def __init__(
self.http_session.mount("http://", _adapter)
self.http_session.mount("https://", _adapter)
self.threading = threading
self.max_workers = max_workers
self.api_version = api_version

self.dcim = App(self, "dcim")
Expand Down
1 change: 1 addition & 0 deletions pynautobot/core/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ def all(self, api_version=None):
token=self.token,
http_session=self.api.http_session,
threading=self.api.threading,
max_workers=self.api.max_workers,
api_version=api_version,
)

Expand Down
6 changes: 5 additions & 1 deletion pynautobot/core/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ class Request(object):
correlate to the filters a given endpoint accepts.
In (e.g. /api/dcim/devices/?name='test') 'name': 'test'
would be in the filters dict.
:param int,optional max_workers: Set the maximum workers for threading in ``.all()``
and ``.filter()`` requests.
"""

def __init__(
Expand All @@ -131,6 +133,7 @@ def __init__(
key=None,
token=None,
threading=False,
max_workers=4,
api_version=None,
):
"""
Expand All @@ -152,6 +155,7 @@ def __init__(
self.http_session = http_session
self.url = self.base if not key else "{}{}/".format(self.base, key)
self.threading = threading
self.max_workers = max_workers
self.api_version = api_version

def get_openapi(self):
Expand Down Expand Up @@ -277,7 +281,7 @@ def _make_call(self, verb="get", url_override=None, add_params=None, data=None):

def concurrent_get(self, ret, page_size, page_offsets):
futures_to_results = []
with cf.ThreadPoolExecutor(max_workers=4) as pool:
with cf.ThreadPoolExecutor(max_workers=self.max_workers) as pool:
for offset in page_offsets:
new_params = {"offset": offset, "limit": page_size}
futures_to_results.append(pool.submit(self._make_call, add_params=new_params))
Expand Down