Skip to content

Commit b4f8cd8

Browse files
authored
chore: check present for async context (#426)
1 parent f216032 commit b4f8cd8

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## 1.28.0 [unreleased]
22

3+
### Features
4+
1. [#413](https://github.com/influxdata/influxdb-client-python/pull/413): Add support for `async/await` with asyncio via `InfluxDBClientAsync`, for more info see: **How to use Asyncio**
5+
36
### Bug Fixes
47
1. [#425](https://github.com/influxdata/influxdb-client-python/pull/425): Improve error message if there is no `organization` with required `name`
58

@@ -9,7 +12,6 @@
912
1. [#412](https://github.com/influxdata/influxdb-client-python/pull/412): `DeleteApi` uses default value from `InfluxDBClient.org` if an `org` parameter is not specified
1013
1. [#405](https://github.com/influxdata/influxdb-client-python/pull/405): Add `InfluxLoggingHandler`. A handler to use the client in native python logging.
1114
1. [#404](https://github.com/influxdata/influxdb-client-python/pull/404): Add `InvocableScriptsApi` to create, update, list, delete and invoke scripts by seamless way
12-
1. [#413](https://github.com/influxdata/influxdb-client-python/pull/413): Add support for `async/await` with asyncio via `InfluxDBClientAsync`, for more info see: **How to use Asyncio**
1315

1416
### Bug Fixes
1517
1. [#419](https://github.com/influxdata/influxdb-client-python/pull/419): Use `allowed_methods` to clear deprecation warning [urllib3]

README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ influxdb-client-python
2828
:target: https://pypi.python.org/pypi/influxdb-client
2929
:alt: Supported Python versions
3030

31-
.. image:: https://readthedocs.org/projects/influxdb-client/badge/?version=latest
32-
:target: https://influxdb-client.readthedocs.io/en/latest/?badge=latest
31+
.. image:: https://readthedocs.org/projects/influxdb-client/badge/?version=stable
32+
:target: https://influxdb-client.readthedocs.io/en/stable/
3333
:alt: Documentation status
3434

3535
.. image:: https://img.shields.io/badge/slack-join_chat-white.svg?logo=slack&style=social

influxdb_client/client/influxdb_client_async.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""InfluxDBClientAsync is client for API defined in https://github.com/influxdata/openapi/blob/master/contracts/oss.yml.""" # noqa: E501
22
import logging
3+
import sys
34

45
from influxdb_client import PingService
56
from influxdb_client.client._base import _BaseClient
@@ -47,6 +48,24 @@ def __init__(self, url, token, org: str = None, debug=None, timeout=10_000, enab
4748
"""
4849
super().__init__(url=url, token=token, org=org, debug=debug, timeout=timeout, enable_gzip=enable_gzip, **kwargs)
4950

51+
# compatibility with Python 3.6
52+
if sys.version_info[:2] >= (3, 7):
53+
from asyncio import get_running_loop
54+
else:
55+
from asyncio import _get_running_loop as get_running_loop
56+
57+
# check present asynchronous context
58+
try:
59+
loop = get_running_loop()
60+
# compatibility with Python 3.6
61+
if loop is None:
62+
raise RuntimeError('no running event loop')
63+
except RuntimeError:
64+
from influxdb_client.client.exceptions import InfluxDBError
65+
message = "The async client should be initialised inside async coroutine " \
66+
"otherwise there can be unexpected behaviour."
67+
raise InfluxDBError(response=None, message=message)
68+
5069
from .._async.api_client import ApiClientAsync
5170
self.api_client = ApiClientAsync(configuration=self.conf, header_name=self.auth_header_name,
5271
header_value=self.auth_header_value, retries=self.retries, **kwargs)

tests/test_InfluxDBClientAsync.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
import os
44
from datetime import datetime
55

6+
import pytest
7+
68
from influxdb_client import Point, WritePrecision
9+
from influxdb_client.client.exceptions import InfluxDBError
710
from influxdb_client.client.influxdb_client_async import InfluxDBClientAsync
811
from tests.base_test import generate_name
912

@@ -223,6 +226,12 @@ async def test_init_from_env(self):
223226

224227
await client_from_envs.close()
225228

229+
def test_initialize_out_side_async_context(self):
230+
with pytest.raises(InfluxDBError) as e:
231+
InfluxDBClientAsync(url="http://localhost:8086", token="my-token", org="my-org")
232+
self.assertEqual("The async client should be initialised inside async coroutine "
233+
"otherwise there can be unexpected behaviour.", e.value.message)
234+
226235

227236
async def _prepare_data(self, measurement: str):
228237
_point1 = Point(measurement).tag("location", "Prague").field("temperature", 25.3)

0 commit comments

Comments
 (0)