Skip to content

initiate transport lazily #34

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,5 @@ docs/_build/

# PyBuilder
target/

.idea/
34 changes: 30 additions & 4 deletions pydrill/client/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-

import os
from threading import RLock

from pydrill.transport import Transport
from pydrill.client.result import ResultQuery, Result, Stats, Profiles
from pydrill.connection.requests_conn import RequestsHttpConnection
Expand All @@ -15,10 +17,34 @@ class PyDrill(object):
"""
# TODO: create better docs.

def __init__(self, host=os.environ.get('PYDRILL_HOST', 'localhost'), port=os.environ.get('PYDRILL_PORT', 8047),
trasport_class=Transport, connection_class=RequestsHttpConnection, auth=None, **kwargs):

self.transport = trasport_class(host, port, connection_class=connection_class, auth=auth, **kwargs)
def __init__(self,
host=os.environ.get('PYDRILL_HOST', 'localhost'),
port=os.environ.get('PYDRILL_PORT', 8047),
trasport_class=Transport,
connection_class=RequestsHttpConnection,
auth=None,
**kwargs):
self.host = host
self.port = port
self.trasport_class = trasport_class
self.connection_class = connection_class
self.auth = auth
self.kwargs = kwargs
self.lock = RLock()
self._transport = None

@property
def transport(self):
# initiate transport lazily
if self._transport is None:
with self.lock:
if self._transport is None:
self._transport = self.trasport_class(self.host, self.port,
connection_class=self.connection_class,
auth=self.auth,
**self.kwargs)
self.lock = None
return self._transport

def perform_request(self, method, url, params=None, body=None):
return self.transport.perform_request(method, url, params, body)
Expand Down