[問題] instance reference /thread profiling
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
Python 近期熱門文章
PTT數位生活區 即時熱門文章