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

Implement Client as context manager #238

Merged
merged 1 commit into from
Jul 22, 2021
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 CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased
### Added
- Support for using `Client` as context manager closing connection on exit. Solves issue [#237](https://github.com/mymarilyn/clickhouse-driver/issues/237).

## [0.2.1] - 2021-06-02
### Added
- Linux wheels for AArch64. Pull request [#197](https://github.com/mymarilyn/clickhouse-driver/pull/197) by [odidev](https://github.com/odidev).
Expand Down
6 changes: 6 additions & 0 deletions clickhouse_driver/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ def __init__(self, *args, **kwargs):
self.reset_last_query()
super(Client, self).__init__()

def __enter__(self):
return self
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could run a dummy query like SELECT 1 here to test the server/client settings (check for unknown settings if settings_is_important)?


def __exit__(self, exc_type, exc_val, exc_tb):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No return value so that exceptions are propagated.

self.disconnect()

def disconnect(self):
"""
Disconnects from the server.
Expand Down
16 changes: 16 additions & 0 deletions docs/features.rst
Original file line number Diff line number Diff line change
Expand Up @@ -423,3 +423,19 @@ Writing pandas DataFrame is also supported with `insert_dataframe`:
... )
>>> client.insert_dataframe('INSERT INTO test VALUES', df)
>>> 10000


Automatic disposal
------------------

*New in version 0.2.2.*

Each Client instance can be used as a context manager:

.. code-block:: python

>>> with Client('localhost') as client:
>>> client.execute('SELECT 1')


Upon exit, any established connection to the ClickHouse server will be closed automatically.
10 changes: 8 additions & 2 deletions tests/test_client.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import ssl
from unittest import TestCase

from clickhouse_driver import Client
from clickhouse_driver.compression.lz4 import Compressor as LZ4Compressor
from clickhouse_driver.compression.lz4hc import Compressor as LZHC4Compressor
from clickhouse_driver.compression.zstd import Compressor as ZSTDCompressor
from clickhouse_driver.protocol import Compression
from tests.numpy.util import check_numpy
from tests.testcase import BaseTestCase


class ClientFromUrlTestCase(TestCase):
class ClientFromUrlTestCase(BaseTestCase):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is the best way to get a Client instance to test with?

def assertHostsEqual(self, client, another, msg=None):
self.assertEqual(list(client.connection.hosts), another, msg=msg)

Expand Down Expand Up @@ -227,3 +227,9 @@ def test_settings_is_important(self):
def test_use_numpy(self):
c = Client.from_url('clickhouse://host?use_numpy=true')
self.assertTrue(c.connection.context.client_settings['use_numpy'])

def test_context_manager(self):
with self.client as c:
c.execute('SELECT 1')
self.assertTrue(c.connection.connected)
self.assertFalse(c.connection.connected)