Skip to content
This repository was archived by the owner on Oct 29, 2024. It is now read-only.

Implemented the context manager protocol #721

Closed
wants to merge 1 commit into from
Closed
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
12 changes: 12 additions & 0 deletions influxdb/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class InfluxDBClient(object):
connect to InfluxDB. Requests can be made to InfluxDB directly through
the client.

The client supports the use as a `context manager
<https://docs.python.org/3/reference/datamodel.html#context-managers>`_.

:param host: hostname to connect to InfluxDB, defaults to 'localhost'
:type host: str
:param port: port to connect to InfluxDB, defaults to 8086
Expand Down Expand Up @@ -61,6 +64,7 @@ class InfluxDBClient(object):
:type proxies: dict
:param path: path of InfluxDB on the server to connect, defaults to ''
:type path: str

"""

def __init__(self,
Expand Down Expand Up @@ -1055,6 +1059,14 @@ def close(self):
if isinstance(self._session, requests.Session):
self._session.close()

def __enter__(self):
"""For use as context manager"""
pass

def __exit__(self, _exc_type, _exc_value, _traceback):
"""For use as context manager"""
self.close()


def _parse_dsn(dsn):
"""Parse data source name.
Expand Down