YasirY
Convex Community4mo ago
2 replies
Yasir

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", {})

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
Was this page helpful?