Câu hỏi này có vẻ khá thú vị và nó đã cho tôi một lý do để nhìn vào Guppy/Heapy, vì điều đó tôi cảm ơn bạn.
Tôi đã thử khoảng 2 giờ để có được Heapy để theo dõi một cuộc gọi/quy trình hàm mà không sửa đổi nguồn của nó bằng số không may mắn.
Tôi đã tìm cách hoàn thành nhiệm vụ của bạn bằng cách sử dụng thư viện Python được xây dựng resource
. Lưu ý rằng tài liệu không chỉ ra giá trị trả về của giá trị RU_MAXRSS
. Một người dùng SO khác là noted rằng nó nằm trong kB. Chạy Mac OSX 7.3 và xem tài nguyên hệ thống của tôi tăng lên trong mã thử nghiệm bên dưới, tôi tin rằng các giá trị trả lại nằm trong Byte, không phải kByte.
Chế độ xem 10000ft về cách tôi sử dụng thư viện resource
để giám sát cuộc gọi thư viện là khởi chạy chức năng trong một chuỗi riêng biệt (theo dõi) và theo dõi tài nguyên hệ thống cho quy trình đó trong chuỗi chính. Dưới đây tôi có hai tệp mà bạn cần chạy để thử nghiệm.
Library Resource Monitor - whatever_you_want.py
import resource
import time
from stoppable_thread import StoppableThread
class MyLibrarySniffingClass(StoppableThread):
def __init__(self, target_lib_call, arg1, arg2):
super(MyLibrarySniffingClass, self).__init__()
self.target_function = target_lib_call
self.arg1 = arg1
self.arg2 = arg2
self.results = None
def startup(self):
# Overload the startup function
print "Calling the Target Library Function..."
def cleanup(self):
# Overload the cleanup function
print "Library Call Complete"
def mainloop(self):
# Start the library Call
self.results = self.target_function(self.arg1, self.arg2)
# Kill the thread when complete
self.stop()
def SomeLongRunningLibraryCall(arg1, arg2):
max_dict_entries = 2500
delay_per_entry = .005
some_large_dictionary = {}
dict_entry_count = 0
while(1):
time.sleep(delay_per_entry)
dict_entry_count += 1
some_large_dictionary[dict_entry_count]=range(10000)
if len(some_large_dictionary) > max_dict_entries:
break
print arg1 + " " + arg2
return "Good Bye World"
if __name__ == "__main__":
# Lib Testing Code
mythread = MyLibrarySniffingClass(SomeLongRunningLibraryCall, "Hello", "World")
mythread.start()
start_mem = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
delta_mem = 0
max_memory = 0
memory_usage_refresh = .005 # Seconds
while(1):
time.sleep(memory_usage_refresh)
delta_mem = (resource.getrusage(resource.RUSAGE_SELF).ru_maxrss) - start_mem
if delta_mem > max_memory:
max_memory = delta_mem
# Uncomment this line to see the memory usuage during run-time
# print "Memory Usage During Call: %d MB" % (delta_mem/1000000.0)
# Check to see if the library call is complete
if mythread.isShutdown():
print mythread.results
break;
print "\nMAX Memory Usage in MB: " + str(round(max_memory/1000.0, 3))
stop Chủ đề - stoppable_thread.py
import threading
import time
class StoppableThread(threading.Thread):
def __init__(self):
super(StoppableThread, self).__init__()
self.daemon = True
self.__monitor = threading.Event()
self.__monitor.set()
self.__has_shutdown = False
def run(self):
'''Overloads the threading.Thread.run'''
# Call the User's Startup functions
self.startup()
# Loop until the thread is stopped
while self.isRunning():
self.mainloop()
# Clean up
self.cleanup()
# Flag to the outside world that the thread has exited
# AND that the cleanup is complete
self.__has_shutdown = True
def stop(self):
self.__monitor.clear()
def isRunning(self):
return self.__monitor.isSet()
def isShutdown(self):
return self.__has_shutdown
###############################
### User Defined Functions ####
###############################
def mainloop(self):
'''
Expected to be overwritten in a subclass!!
Note that Stoppable while(1) is handled in the built in "run".
'''
pass
def startup(self):
'''Expected to be overwritten in a subclass!!'''
pass
def cleanup(self):
'''Expected to be overwritten in a subclass!!'''
pass
Nguồn
2012-04-12 05:05:13
Cảm ơn bạn đã trả lời chi tiết! – astrofrog
@astrofrog Rất vui được giúp đỡ. Đó là điều mà tôi cũng có thể hưởng lợi trong tương lai. –
Tôi đặt mã này như một ý chính để dễ dàng tải xuống các tệp: https://gist.github.com/b54fafd87634f017d50d – Tom