Yasir
Yasir2mo ago

Python Client: Is set_auth() thread-safe for concurrent requests?

Context: I'm building a FastAPI backend that serves multiple concurrent users. To optimize Convex WebSocket connection overhead (~400-600ms per new connection), I want to reuse a single ConvexClient instance across requests and call set_auth(user_token) per-request. Current Setup:
# Per-request (current - slow but safe)
def handle_request(user_token: str):
client = ConvexClient(CONVEX_URL) # New connection each time
client.set_auth(user_token)
return client.query("getPrivateData", {})

# Proposed (fast but safe?)
shared_client = ConvexClient(CONVEX_URL) # Shared across requests

def handle_request(user_token: str):
shared_client.set_auth(user_token) # Thread-safe?
return shared_client.query("getPrivateData", {})
# Per-request (current - slow but safe)
def handle_request(user_token: str):
client = ConvexClient(CONVEX_URL) # New connection each time
client.set_auth(user_token)
return client.query("getPrivateData", {})

# Proposed (fast but safe?)
shared_client = ConvexClient(CONVEX_URL) # Shared across requests

def handle_request(user_token: str):
shared_client.set_auth(user_token) # Thread-safe?
return shared_client.query("getPrivateData", {})
Question: Is ConvexClient.set_auth(token) thread-safe in the Python client (v0.7.0)? Specifically: • If Request A calls set_auth(token_A) followed by Request B calling set_auth(token_B) before A's query executes, will A's query use the correct token? • Does set_auth() mutate shared connection-level state, or is auth tracked per-query somehow? What I'm seeing: • Per-request clients work but have high latency (965ms per query) • Shared client would reduce latency, but I need to confirm data isolation between users What I'd like: Either confirmation that shared client + per-request set_auth() is safe, OR recommended approach for connection pooling in multi-threaded Python environments. Thanks! ────────────────────────────────────────── • Python client: 0.7.0 • Python: 3.13
2 Replies
Convex Bot
Convex Bot2mo ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
Yasir
YasirOP2mo ago
It is crucial blocker for my app development. Can someone from convex team please guide?

Did you find this page helpful?