Python 3.x 如何在Python函数调用期间监视mem的使用
我有一个Python函数,我想监视它随时间的内存使用情况,称之为foo 请注意,我不想使用外部探查器。。。我只需要非常简单的统计数据,我可以附加到日志文件 我想在foo主体执行时,使用以下函数在单独的流程中执行监视:Python 3.x 如何在Python函数调用期间监视mem的使用,python-3.x,memory,Python 3.x,Memory,我有一个Python函数,我想监视它随时间的内存使用情况,称之为foo 请注意,我不想使用外部探查器。。。我只需要非常简单的统计数据,我可以附加到日志文件 我想在foo主体执行时,使用以下函数在单独的流程中执行监视: def monitor_mem(monitor_list, tic): while process_not_terminated: monitor_list.append(psutil.virtual_memory()) time.sleep
def monitor_mem(monitor_list, tic):
while process_not_terminated:
monitor_list.append(psutil.virtual_memory())
time.sleep(tic)
foo函数类似于以下内容:注意,mp是多处理模块:
我在monitor_mem中做了一段时间的循环。这似乎不起作用。我觉得我需要能够发送某种信号来停止功能。。。但我只是不确定。希望你们中的一位能够看看我的代码示例并给我一些提示。谢谢 在处理线程和进程时,应该使用队列而不是列表 队列的示例:
from multiprocessing import Process, Queue
from datetime import datetime
from time import sleep
def monitor_mem(monitor_queue):
while True:
monitor_queue.put(datetime.now())
sleep(1)
queue = Queue()
process = Process(target=monitor_mem, args=(queue,))
process.daemon = True
process.start()
print("Doing something")
sleep(5)
process.terminate()
while not queue.empty():
item = queue.get()
print(item)
from multiprocessing import Process, Queue
from datetime import datetime
from time import sleep
def monitor_mem(monitor_queue):
while True:
monitor_queue.put(datetime.now())
sleep(1)
queue = Queue()
process = Process(target=monitor_mem, args=(queue,))
process.daemon = True
process.start()
print("Doing something")
sleep(5)
process.terminate()
while not queue.empty():
item = queue.get()
print(item)