Line | |
---|
1 | import sys |
---|
2 | import traceback |
---|
3 | import signal |
---|
4 | import threading |
---|
5 | |
---|
6 | from twisted.internet import reactor |
---|
7 | |
---|
8 | |
---|
9 | def print_stacks(): |
---|
10 | print("Uh oh, something is blocking the event loop!") |
---|
11 | current_thread = threading.get_ident() |
---|
12 | for thread_id, frame in sys._current_frames().items(): |
---|
13 | if thread_id == current_thread: |
---|
14 | traceback.print_stack(frame, limit=10) |
---|
15 | break |
---|
16 | |
---|
17 | |
---|
18 | def catch_blocking_in_event_loop(test=None): |
---|
19 | """ |
---|
20 | Print tracebacks if the event loop is blocked for more than a short amount |
---|
21 | of time. |
---|
22 | """ |
---|
23 | signal.signal(signal.SIGALRM, lambda *args: print_stacks()) |
---|
24 | |
---|
25 | current_scheduled = [None] |
---|
26 | |
---|
27 | def cancel_and_rerun(): |
---|
28 | signal.setitimer(signal.ITIMER_REAL, 0) |
---|
29 | signal.setitimer(signal.ITIMER_REAL, 0.015) |
---|
30 | current_scheduled[0] = reactor.callLater(0.01, cancel_and_rerun) |
---|
31 | |
---|
32 | cancel_and_rerun() |
---|
33 | |
---|
34 | def cleanup(): |
---|
35 | signal.signal(signal.SIGALRM, signal.SIG_DFL) |
---|
36 | signal.setitimer(signal.ITIMER_REAL, 0) |
---|
37 | current_scheduled[0].cancel() |
---|
38 | |
---|
39 | if test is not None: |
---|
40 | test.addCleanup(cleanup) |
---|
Note: See
TracBrowser
for help on using the repository browser.