diff --git a/CHANGELOG.md b/CHANGELOG.md index bf578852..a77c5418 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ ## 1.28.0 [unreleased] +### Features +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** + ### Bug Fixes 1. [#425](https://github.com/influxdata/influxdb-client-python/pull/425): Improve error message if there is no `organization` with required `name` @@ -9,7 +12,6 @@ 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 1. [#405](https://github.com/influxdata/influxdb-client-python/pull/405): Add `InfluxLoggingHandler`. A handler to use the client in native python logging. 1. [#404](https://github.com/influxdata/influxdb-client-python/pull/404): Add `InvocableScriptsApi` to create, update, list, delete and invoke scripts by seamless way -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** ### Bug Fixes 1. [#419](https://github.com/influxdata/influxdb-client-python/pull/419): Use `allowed_methods` to clear deprecation warning [urllib3] diff --git a/README.rst b/README.rst index 7a371189..a2074e7f 100644 --- a/README.rst +++ b/README.rst @@ -28,8 +28,8 @@ influxdb-client-python :target: https://pypi.python.org/pypi/influxdb-client :alt: Supported Python versions -.. image:: https://readthedocs.org/projects/influxdb-client/badge/?version=latest - :target: https://influxdb-client.readthedocs.io/en/latest/?badge=latest +.. image:: https://readthedocs.org/projects/influxdb-client/badge/?version=stable + :target: https://influxdb-client.readthedocs.io/en/stable/ :alt: Documentation status .. image:: https://img.shields.io/badge/slack-join_chat-white.svg?logo=slack&style=social diff --git a/influxdb_client/client/influxdb_client_async.py b/influxdb_client/client/influxdb_client_async.py index 43465c7d..fd35ade0 100644 --- a/influxdb_client/client/influxdb_client_async.py +++ b/influxdb_client/client/influxdb_client_async.py @@ -1,5 +1,6 @@ """InfluxDBClientAsync is client for API defined in https://github.com/influxdata/openapi/blob/master/contracts/oss.yml.""" # noqa: E501 import logging +import sys from influxdb_client import PingService 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 """ super().__init__(url=url, token=token, org=org, debug=debug, timeout=timeout, enable_gzip=enable_gzip, **kwargs) + # compatibility with Python 3.6 + if sys.version_info[:2] >= (3, 7): + from asyncio import get_running_loop + else: + from asyncio import _get_running_loop as get_running_loop + + # check present asynchronous context + try: + loop = get_running_loop() + # compatibility with Python 3.6 + if loop is None: + raise RuntimeError('no running event loop') + except RuntimeError: + from influxdb_client.client.exceptions import InfluxDBError + message = "The async client should be initialised inside async coroutine " \ + "otherwise there can be unexpected behaviour." + raise InfluxDBError(response=None, message=message) + from .._async.api_client import ApiClientAsync self.api_client = ApiClientAsync(configuration=self.conf, header_name=self.auth_header_name, header_value=self.auth_header_value, retries=self.retries, **kwargs) diff --git a/tests/test_InfluxDBClientAsync.py b/tests/test_InfluxDBClientAsync.py index 92cb64f0..662c4437 100644 --- a/tests/test_InfluxDBClientAsync.py +++ b/tests/test_InfluxDBClientAsync.py @@ -3,7 +3,10 @@ import os from datetime import datetime +import pytest + from influxdb_client import Point, WritePrecision +from influxdb_client.client.exceptions import InfluxDBError from influxdb_client.client.influxdb_client_async import InfluxDBClientAsync from tests.base_test import generate_name @@ -223,6 +226,12 @@ async def test_init_from_env(self): await client_from_envs.close() + def test_initialize_out_side_async_context(self): + with pytest.raises(InfluxDBError) as e: + InfluxDBClientAsync(url="http://localhost:8086", token="my-token", org="my-org") + self.assertEqual("The async client should be initialised inside async coroutine " + "otherwise there can be unexpected behaviour.", e.value.message) + async def _prepare_data(self, measurement: str): _point1 = Point(measurement).tag("location", "Prague").field("temperature", 25.3)