Update database.py

Added connection timeout parameter
This commit is contained in:
P R 2018-11-28 00:09:53 +07:00 committed by GitHub
parent 41cf4c3a79
commit 75f0ad9882
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -73,7 +73,7 @@ class Database(object):
''' '''
def __init__(self, db_name, db_url='http://localhost:8123/', def __init__(self, db_name, db_url='http://localhost:8123/',
username=None, password=None, readonly=False, autocreate=True): username=None, password=None, readonly=False, autocreate=True, timeout=60):
''' '''
Initializes a database instance. Unless it's readonly, the database will be Initializes a database instance. Unless it's readonly, the database will be
created on the ClickHouse server if it does not already exist. created on the ClickHouse server if it does not already exist.
@ -84,12 +84,14 @@ class Database(object):
- `password`: optional connection credentials. - `password`: optional connection credentials.
- `readonly`: use a read-only connection. - `readonly`: use a read-only connection.
- `autocreate`: automatically create the database if does not exist (unless in readonly mode). - `autocreate`: automatically create the database if does not exist (unless in readonly mode).
- `timeout`: the connect timeout.
''' '''
self.db_name = db_name self.db_name = db_name
self.db_url = db_url self.db_url = db_url
self.username = username self.username = username
self.password = password self.password = password
self.readonly = False self.readonly = False
self.timeout = timeout
self.settings = {} self.settings = {}
self.db_exists = False self.db_exists = False
self.db_exists = self._is_existing_database() self.db_exists = self._is_existing_database()
@ -319,7 +321,8 @@ class Database(object):
if isinstance(data, string_types): if isinstance(data, string_types):
data = data.encode('utf-8') data = data.encode('utf-8')
params = self._build_params(settings) params = self._build_params(settings)
r = requests.post(self.db_url, params=params, data=data, stream=stream) timeout = self.timeout
r = requests.post(self.db_url, params=params, data=data, stream=stream, timeout=timeout)
if r.status_code != 200: if r.status_code != 200:
raise ServerError(r.text) raise ServerError(r.text)
return r return r