Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/threadframe/test.py
blob: 6f1c82ab5abdac9abdf0deefde7ac82946c96363 (plain)
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import sys, time, threading, thread, os, traceback, threadframe, pprint
# daemon thread that spouts Monty Pythonesque nonsense
class T(threading.Thread):
  def run(self):
    while 1:
      time.sleep(1)
      print '[%d] Spam spam spam spam. Lovely spam! Wonderful spam!' % ( thread.get_ident(), )
# thread that cause a deliberate deadlock with itself
U_lock = threading.Lock()
class U(threading.Thread):
  def run(self):
    U_lock.acquire()
    U_lock.acquire()
# thread that will exit after the thread frames are extracted but before
# they are printed
V_event = threading.Event()
class V(threading.Thread):
  def run(self):
    V_event.clear()
    V_event.wait()
    
print 'ident of main thread is: %d' % (thread.get_ident(),)
print
print 'launching daemon thread...',
T().start()
print 'done'
print 'launching self-deadlocking thread...',
U().start()
print 'done'
print 'launching thread that will die before the end...',
v = V()
v.start()
print 'done'

time.sleep(5)

# Python 2.2 does not support threadframe.dict()
if sys.hexversion < 0x02030000:
  frames = threadframe.threadframe()
else:
  frames = threadframe.dict()

# signal the thread V to die, then wait for it to oblige
V_event.set()
v.join()

if sys.hexversion < 0x02030000:
  for frame in frames:
    print '-' * 72
    print 'frame ref count = %d' % sys.getrefcount(frame)
    traceback.print_stack(frame)
else:
  for thread_id, frame in frames.iteritems():
    print '-' * 72
    print '[%s] %d' % (thread_id, sys.getrefcount(frame))
    traceback.print_stack(frame)
os._exit(0)