You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+50-1Lines changed: 50 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,7 +21,56 @@ When using the standard Cassandra driver in async applications, blocking operati
21
21
22
22
In async Python applications, an **event loop** manages all operations in a single thread. Think of it like a smart traffic controller - it efficiently switches between tasks whenever one is waiting for I/O (like a database query). This allows handling thousands of concurrent requests without creating thousands of threads.
23
23
24
-
However, when you use a **synchronous (blocking)** operation in an async application, it's like that traffic controller suddenly freezing - all traffic stops until that one operation completes. If a database query takes 100ms and blocks the event loop, that means your web server can't process ANY other requests during those 100ms. This is why the standard Cassandra driver's thread pool approach doesn't work well with async frameworks.
24
+
**The key issue**: When you use the standard Cassandra driver (which is synchronous) inside an async web framework like FastAPI or aiohttp, you create a blocking problem. The synchronous driver operations block the event loop, preventing your async application from handling other requests.
25
+
26
+
**Important clarification**: This blocking issue only occurs when:
27
+
1. You're building an async application (using FastAPI, aiohttp, Quart, etc.)
28
+
2. You use synchronous database operations inside async handlers
29
+
30
+
If you're building a traditional synchronous application (Flask, Django without async views), the standard Cassandra driver works fine and you don't need this wrapper.
31
+
32
+
Here's a concrete example showing the problem and solution:
33
+
34
+
```python
35
+
# ❌ Synchronous code in an async handler - BLOCKS the event loop
36
+
from fastapi import FastAPI
37
+
from cassandra.cluster import Cluster
38
+
39
+
app = FastAPI()
40
+
cluster = Cluster(['localhost'])
41
+
session = cluster.connect()
42
+
43
+
@app.get("/users/{user_id}")
44
+
asyncdefget_user(user_id: str):
45
+
# This blocks the event loop! While this query runs,
46
+
# your FastAPI app cannot process ANY other requests
47
+
result = session.execute("SELECT * FROM users WHERE id = %s", [user_id])
48
+
return {"user": result.one()}
49
+
```
50
+
51
+
```python
52
+
# ✅ Async code with our wrapper - NON-BLOCKING
53
+
from fastapi import FastAPI
54
+
from async_cassandra import AsyncCluster
55
+
56
+
app = FastAPI()
57
+
cluster = AsyncCluster(['localhost'])
58
+
session =None
59
+
60
+
@app.on_event("startup")
61
+
asyncdefstartup():
62
+
global session
63
+
session =await cluster.connect()
64
+
65
+
@app.get("/users/{user_id}")
66
+
asyncdefget_user(user_id: str):
67
+
# This doesn't block! The event loop remains free to handle
68
+
# other requests while waiting for the database response
69
+
result =await session.execute("SELECT * FROM users WHERE id = %s", [user_id])
70
+
return {"user": result.one()}
71
+
```
72
+
73
+
The key difference: with the sync driver, each database query blocks your entire async application from handling other requests. With async-cassandra, the event loop remains free to process other work while waiting for database responses.
0 commit comments