@@ -8,7 +8,69 @@ Get aiocop running in 5 minutes.
88pip install aiocop
99```
1010
11- ## Basic Setup
11+ ## Try It Now
12+
13+ Copy this into a file and run it - no dependencies needed:
14+
15+ ``` python
16+ # test_aiocop.py
17+ import asyncio
18+ import aiocop
19+
20+
21+ def on_slow_task (event ):
22+ print (f " SLOW TASK DETECTED: { event.elapsed_ms:.1f } ms " )
23+ print (f " Severity: { event.severity_level} " )
24+ print (f " Blocking events: { len (event.blocking_events)} " )
25+ for evt in event.blocking_events:
26+ print (f " - { evt[' event' ]} at { evt[' entry_point' ]} " )
27+
28+
29+ async def blocking_task ():
30+ print (" Executing task..." )
31+ # This synchronous open() will block the loop!
32+ with open (" /dev/null" , " w" ) as f:
33+ f.write(" data" )
34+ await asyncio.sleep(0.1 )
35+
36+
37+ async def main ():
38+ # Setup aiocop
39+ aiocop.patch_audit_functions()
40+ aiocop.start_blocking_io_detection(trace_depth = 5 )
41+ aiocop.detect_slow_tasks(threshold_ms = 10 , on_slow_task = on_slow_task)
42+ aiocop.activate()
43+
44+ # Run your code as normal
45+ await asyncio.gather(blocking_task(), blocking_task())
46+
47+ aiocop.deactivate()
48+
49+
50+ if __name__ == " __main__" :
51+ asyncio.run(main())
52+ ```
53+
54+ ``` bash
55+ python test_aiocop.py
56+ ```
57+
58+ ** Output:**
59+
60+ ```
61+ Executing task...
62+ Executing task...
63+ SLOW TASK DETECTED: 102.3ms
64+ Severity: medium
65+ Blocking events: 1
66+ - open(/dev/null, w) at test_aiocop.py:15:blocking_task
67+ SLOW TASK DETECTED: 103.1ms
68+ Severity: medium
69+ Blocking events: 1
70+ - open(/dev/null, w) at test_aiocop.py:15:blocking_task
71+ ```
72+
73+ ## Understanding the Setup
1274
1375aiocop requires three setup steps, then activation:
1476
@@ -30,84 +92,25 @@ aiocop.activate()
3092
3193That's it! aiocop is now monitoring your async code.
3294
33- ## Adding a Callback
95+ ## The Callback
3496
35- To actually see the detected events, register a callback :
97+ The callback receives a ` SlowTaskEvent ` with all the details :
3698
3799``` python
38- import aiocop
39-
40100def on_slow_task (event : aiocop.SlowTaskEvent) -> None :
41101 # Callback is invoked for ALL blocking I/O, not just slow tasks.
42102 # Use exceeded_threshold to check if it was actually slow.
43103 if event.exceeded_threshold:
44104 print (f " SLOW TASK: { event.elapsed_ms:.1f } ms (threshold: { event.threshold_ms} ms) " )
45- print (f " Severity: { event.severity_level} (score: { event.severity_score} ) " )
46- print (f " Reason: { event.reason} " )
47-
48- for evt in event.blocking_events:
49- print (f " - { evt[' event' ]} " )
50- print (f " at { evt[' trace' ]} " )
51-
52- # Setup
53- aiocop.patch_audit_functions()
54- aiocop.start_blocking_io_detection()
55- aiocop.detect_slow_tasks(threshold_ms = 30 , on_slow_task = on_slow_task)
56- aiocop.activate()
57- ```
58-
59- ** Note:** The callback is invoked for ** all tasks with blocking I/O detected** , even fast ones. Check ` event.exceeded_threshold ` to filter for slow tasks only.
105+ print (f " Severity: { event.severity_level} (score: { event.severity_score} ) " )
106+ print (f " Reason: { event.reason} " )
60107
61- ## Complete Example
62-
63- Here's a complete example that demonstrates aiocop detecting blocking I/O:
64-
65- ``` python
66- import asyncio
67- import time
68- import aiocop
69-
70- def on_slow_task (event : aiocop.SlowTaskEvent) -> None :
71- if event.exceeded_threshold:
72- print (f " \n Blocking detected! " )
73- print (f " Duration: { event.elapsed_ms:.1f } ms " )
74- print (f " Severity: { event.severity_level} " )
75108 for evt in event.blocking_events:
76- print (f " - { evt[' event' ]} " )
77-
78- async def bad_async_function ():
79- """ This function has a blocking call - aiocop will detect it!"""
80- await asyncio.sleep(0.01 ) # This is fine (async)
81- time.sleep(0.05 ) # This is BAD (blocking) - aiocop will catch it!
82- await asyncio.sleep(0.01 ) # This is fine (async)
83-
84- async def main ():
85- # Setup aiocop
86- aiocop.patch_audit_functions()
87- aiocop.start_blocking_io_detection()
88- aiocop.detect_slow_tasks(threshold_ms = 30 , on_slow_task = on_slow_task)
89- aiocop.activate()
90-
91- print (" Running async task with blocking call..." )
92- await bad_async_function()
93- print (" \n Done!" )
94-
95- if __name__ == " __main__" :
96- asyncio.run(main())
97- ```
98-
99- ** Output:**
100-
109+ print (f " - { evt[' event' ]} " )
110+ print (f " at { evt[' trace' ]} " )
101111```
102- Running async task with blocking call...
103-
104- Blocking detected!
105- Duration: 52.3ms
106- Severity: high
107- - time.sleep(0.05)
108112
109- Done!
110- ```
113+ ** Note:** The callback is invoked for ** all tasks with blocking I/O detected** , even fast ones. Check ` event.exceeded_threshold ` to filter for slow tasks only.
111114
112115## What Gets Detected?
113116
0 commit comments