Sync vs async
Three tiers, two of them transports for the same protocol — how AsyncBrainClient differs from BrainClient, and why you pick one per program.
Three tiers, not two transports
BrainClientwire · synchronousBlocking calls over the BRN0 protocol. A background reader thread
demultiplexes responses by stream_id, so one client serves requests from
several threads at once.
AsyncBrainClientwire · asyncioThe same verbs, each a coroutine. A reader task does the demultiplexing.
BrainHttpClientHTTP · synchronousA separate thing — the hosted-edge REST client, not a transport choice for the same API. See HTTP client.
The synchronous client blocks the event loop when called from inside a coroutine — the calls succeed, and every other task on that loop stalls for the duration. The async one needs a running loop and raises without one. Neither failure is loud, which is why the choice belongs at the top of a program rather than per call site.
What changes
The close methods have different names
AsyncBrainClient closes with aclose(), not close() — the asyncio
convention, and a deliberate difference so a copy-pasted client.close()
fails loudly instead of silently doing nothing.
Everything else is shared: the same ClientConfig, the same Auth, the same
request dataclasses, the same builders, the same error hierarchy, the same
ConnectionInfo.
Side by side
# Synchronous
from brain_db_sdk import BrainClient, Auth, EncodeBuilder, RecallBuilder
client = BrainClient.connect("127.0.0.1", 9090, Auth.token(key))
try:
client.encode(EncodeBuilder("Priya moved to Lisbon.").session(42).build())
answer = client.recall(RecallBuilder("where does Priya work?").limit(5).build())
finally:
client.close()# asyncio
from brain_db_sdk import AsyncBrainClient, Auth, EncodeBuilder, RecallBuilder
client = await AsyncBrainClient.connect("127.0.0.1", 9090, Auth.token(key))
try:
await client.encode(EncodeBuilder("Priya moved to Lisbon.").session(42).build())
answer = await client.recall(RecallBuilder("where does Priya work?").limit(5).build())
finally:
await client.aclose()The builders are not async and never were — they construct a request locally. Only the verbs are coroutines.
Concurrency
Both tiers multiplex: several requests can be in flight on one connection, and you do not manage streams. The difference is how you express the concurrency.
# Synchronous — threads
from concurrent.futures import ThreadPoolExecutor
with ThreadPoolExecutor() as pool:
answer, entities = pool.map(
lambda f: f(), [lambda: client.recall(req), lambda: client.list_entities(list_req)]
)# asyncio — gather
answer, entities = await asyncio.gather(
client.recall(req),
client.list_entities(list_req),
)Subscriptions
AsyncSubscription is an async iterator; the synchronous Subscription is an
ordinary one and also a context manager.
# asyncio
sub = await client.subscribe(request)
async for event in sub:
last_lsn = event.lsn
handle(event)
await sub.unsubscribe()See subscribe for the filter, resumption by
LSN, and the difference between unsubscribe() and close().
Pools
AsyncPool mirrors Pool: connect, connect_with, get, get_healthy,
size and close, each awaited where the synchronous version blocks. Prefer
get_healthy for anything long-lived — see
Wire client.
Was this page helpful?