Conversation
genzgd
left a comment
There was a problem hiding this comment.
This seems okay to me, although I can't claim to have done anything resembling a full review. A couple observations:
- I'm curious as to where the improvements come from over the existing implementation, so I'm looking forward to that blog post.
- There's a lot of duplicated code in the aiohttp_client. It would be nice to consolidate that somewhere.
- The piece with the async queue is hard to follow -- I don't know out feasible it is, but it would be nice to remove that layer and just use some kind of async based generator without wrapping the extra queue.
|
Thanks @genzgd. To address your questions:
|
Yes, as I think about it, that makes sense. It might be theoretically possible to run the sync HTTP client (and the buffer) in a separate thread than the parser, gaining a similar benefit. On a related note, making the transform step truly parallel would be challenging given the fact that HTTP chunks won't align with Native format blocks, but that's another argument in favor of a TCP protocol client. :) |
Summary
This PR adds a native async HTTP client based on
aiohttp(AiohttpAsyncClientinclickhouse_connect/driver/aiohttp_client.py) and wires it into the async client factory path. It replaces the default executor-wrapped sync client with true async I/O while keeping the public async API consistent with the existingAsyncClientsurface. The legacy executor-wrapped path remains available (and is now explicitly deprecated) when passing a sync client.Why this change
The previous async client was a thin wrapper around sync operations executed in a thread pool, which:
The new implementation performs HTTP I/O natively with
aiohttpwhile preserving the established client API and behavior.Key behavior and design points
Native async I/O with aiohttp
Requests use
aiohttp.ClientSessionwith a configurableTCPConnector(pool limits, keepalive). HTTP response handling is fully async.Streaming bridge for Native format
Native format parsing/serialization is still synchronous CPU-bound work. The client uses a bounded queue as a sync/async bridge so async network reads/writes can overlap with sync parsing/serialization in an executor.
On the async query path (
StreamingResponseSource), he async producer reads fromaiohttpresponse and the sync consumer parses in an executor.On the async insert path (
StreamingInsertSource), the sync producer serializes in an executor and the async consumer streams toaiohttp.Preventing event loop blocking
The client uses two complementary strategies to prevent users from accidentally blocking the event loop.
For non-streaming queries (
.query(),.query_df(), etc.) results are fully materialized inside the executor before returning to the event loop. By the time aQueryResultis returned, all data is already in memory, so synchronous iteration is safe and won't cause deadlocks.For streaming queries (
.query_rows_stream(),.query_df_stream(), etc.) theAsyncSyncQueuebridge actively detects deadlock attempts. If you try to synchronously iterate a stream (for row in stream) from within anasync deffunction, it raises aProgrammingErrorimmediately, prompting you to useasync forinstead.Backward compatibility
AsyncClient(client=...)still wraps the sync client in an executor, but emits a deprecation warning. The recommended path isget_async_client(...), which now creates the aiohttp-based client.Tests
Integration tests using
param_clientnow exercise both sync and async clients, and new async-specific tests validate native async behaviors (concurrency, streaming cleanup, session protection, timeouts, etc.).Migration and compatibility notes
async_client = await clickhouse_connect.get_async_client(...)AsyncClient(client=sync_client)Notable trade-offs
Performance notes
A preliminary benchmark comparing the executor-based async client (as it exists in clickhouse-connect v0.10.0) against the new async-native client was performed. The setup was as follows:
The observed speedups of the new async client over the executor-based client ranged from 2% to 95% with average increase of around 40%, depending on the workload. P95 latencies showed marked improvement as well. A detailed design/benchmark blog post is planned and a link will be provided when done.
Checklist
Delete items not relevant to your PR: