-
Notifications
You must be signed in to change notification settings - Fork 110
Implement native async client #617
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
Open
joe-clickhouse
wants to merge
32
commits into
main
Choose a base branch
from
joe/141-a-database-client-should-be-based-on-asyncio
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Implement native async client #617
joe-clickhouse
wants to merge
32
commits into
main
from
joe/141-a-database-client-should-be-based-on-asyncio
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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: