Skip to content

Batch write example doesn't wait for callbacks and batch client doesn't support async/await #103

@jstirnaman

Description

@jstirnaman

Specifications

  • Client Version: v0.7.0
  • InfluxDB Version: Cloud Dedicated
  • Platform: MacOS

Code sample to reproduce problem

Using the example in the README, I now need to insert a time.sleep call of 2-3 seconds to make sure the callbacks have run.
I didn't encounter this until recently (using v0.5.0) so I don't know what's changed--maybe the InfluxDB instance is slower.

However, if I try to use async/await with the client, then I get the following error:

'InfluxDBClient3' object does not support the asynchronous context manager protocol`

Example using async/await:

from influxdb_client_3 import(InfluxDBClient3,
                              write_client_options,
                              WriteOptions,
                              InfluxDBError)

class BatchingCallback(object):

    def __init__(self):
        self.status = None

    def success(self, conf: tuple, data=None):
        self.status = f"Success: Data has been successfully written: {data}"
        print(self.status)

    def error(self, conf: tuple, data: str, err: InfluxDBError):
        self.status = f"{err}: Error writing batch: config: {conf}, data: {data}"
        print(self.status)

    def retry(self, conf: tuple, data: str, err: InfluxDBError):
        self.status = f"{err}: Retry error writing batch: config: {conf}, data: {data}"
        print(self.status)

import asyncio

async def main():
    callback = BatchingCallback()

    # Instantiate default WriteOptions for batching
    write_options = WriteOptions()
    wco = write_client_options(success_callback=callback.success,
                               error_callback=callback.error,
                               retry_callback=callback.retry,
                               write_options=write_options)

    # Use the with...as statement to ensure the file is properly closed and resources
    # are released.
    async with InfluxDBClient3(host="your_influxdb_host",
                               database="DATABASE_NAME",
                               token="DATABASE_TOKEN",
                               write_client_options=wco) as client:
        client._client.debug = True  # Enable debug mode
        await client.write_file(file='./data/home-sensor-data.csv',
                                timestamp_column='time',
                                tag_columns=["room"],
                                write_precision='s')

        assert callback.status.startswith("Success"), f"{callback.status}"

# Run the main function
asyncio.run(main())

Expected behavior

Using with and a batch write client, the client should wait for the response and for callbacks to fire.

Actual behavior

Execution continues without callbacks having run, and the client doesn't support async/await.

Additional info

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions