-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsys_current_frames.py
More file actions
40 lines (34 loc) · 1.05 KB
/
Copy pathsys_current_frames.py
File metadata and controls
40 lines (34 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env python
# encoding: utf-8
import sys
import threading
import time
io_lock = threading.Lock()
blocker = threading.Lock()
def block(i):
t = threading.current_thread()
with io_lock:
print '%s with ident %s going to sleep' % (t.name, t.ident)
if i:
blocker.acquire() # acquired but never released
time.sleep(0.2)
with io_lock:
print t.name, 'finishing'
return
# Create and start several threads that "block"
threads = [ threading.Thread(target=block, args=(i,)) for i in range(3) ]
for t in threads:
t.setDaemon(True)
t.start()
# Map the threads from their identifier to the thread object
threads_by_ident = dict((t.ident, t) for t in threads)
# Show where each thread is "blocked"
time.sleep(0.01)
with io_lock:
for ident, frame in sys._current_frames().items():
t = threads_by_ident.get(ident)
if not t:
# Main thread
continue
print t.name, 'stopped in', frame.f_code.co_name,
print 'at line', frame.f_lineno, 'of', frame.f_code.co_filename