作者sean72 (.)
看板Python
标题[问题] instance reference /thread profiling
时间Fri Jul 26 09:49:17 2013
Import yappi
class Car(threading.Thread):
Car_count = 0
def __init__(self, miles = 30):
threading.Thread.__init__(self)
self.miles = miles
self.__class__.Car_count += 1
self.car_id = self.__class__.Car_count
self.start()
def run(self):
temp = 0
for idx in range(self.miles):
temp = temp +1
# CPU profiling
print('========== Thread: %d ==========' % (self.car_id) )
stats = yappi.get_stats()
for n in stats.thread_stats:
print(n)
if __name__ == '__main__':
yappi.start()
toyota = Car(100)
honda = Car(100000000)
使用yappi去做thread profiling
当honda还在跑的时候 toyota应该已经结束了
请问结束的瞬间,toyota 就会瞬间被del吗? (我心里预期如此)
下面是profiling的结果
参数分别代表
('thread_name', 'thread_id', 'last_func', 'total time spent', 'sched_count')
Thread 1是在预期之中
可是在thread 2里面印出来的结果却发现
我有两个Car thread, 而且存在的时间几乎一样久 ?
========== Thread: 1 ==========
{0: '_MainThread', 1: 3073361600, 2:
'/usr/lib/python3.2/threading.py._release_save:211', 3: 0.0, 4: 1}
{0: 'Car', 1: 3069139776, 2:
'/usr/local/lib/python3.2/dist-packages/yappi.py.__init__:50', 3:
0.000545762, 4: 1}
========== Thread: 2 ==========
{0: 'Car', 1: 3058694976, 2:
'/usr/local/lib/python3.2/dist-packages/yappi.py.enum_thread_stats:128', 3:
6.373563289000001, 4: 1}
{0: '_MainThread', 1: 3073361600, 2:
'/usr/lib/python3.2/threading.py._release_save:211', 3: 6.334833099000001, 4:
2}
{0: 'Car', 1: 3069139776, 2: '/usr/lib/python3.2/threading.py._note:50', 3:
6.373548762, 4: 1}
但是当我再两个class中间加入一个短暂的sleep之後
toyota = Car(100)
time.sleep(0.01)
honda = Car(100000000)
印出来的结果就满符合预期的
Thread 1先跑先结束
Thread 2 执行的时候也不会看到thread 1了(因为已经del)
========== Thread: 1 ==========
{0: '_MainThread', 1: 3073447616, 2:
'/usr/lib/python3.2/threading.py._acquire_restore:214', 3: 0.0, 4: 1}
{0: 'Car', 1: 3069225792, 2:
'/usr/local/lib/python3.2/dist-packages/yappi.py.__init__:50', 3:
0.0005412430000000001, 4: 1}
========== Thread: 2 ==========
{0: '_MainThread', 1: 3073447616, 2:
'/usr/lib/python3.2/threading.py._release_save:211', 3: 6.317869170000001, 4:
2}
{0: 'Car', 1: 3069225792, 2:
'/usr/local/lib/python3.2/dist-packages/yappi.py.__init__:50', 3:
6.355446563, 4: 2}
请问有人能帮忙分析一下吗?
thanks
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 172.249.127.149