[英]memory usage with python multiprocessing
进程加入后, multiprocessing.Process
处理过程产生的进程消耗的内存是否会被释放?
我想到的场景大致如下:
from multiprocessing import Process
from multiprocessing import Queue
import time
import os
def main():
tasks = Queue()
for task in [1, 18, 1, 2, 5, 2]:
tasks.put(task)
num_proc = 3 # this many workers @ each point in time
procs = []
for j in range(num_proc):
p = Process(target = run_q, args = (tasks,))
procs.append(p)
p.start()
# joines a worker once he's done
while procs:
for p in procs:
if not p.is_alive():
p.join() # what happens to the memory allocated by run()?
procs.remove(p)
print p, len(procs)
time.sleep(1)
def run_q(task_q):
while not task_q.empty(): # while's stuff to do, keep working
task = task_q.get()
run(task)
def run(x): # do real work, allocates memory
print x, os.getpid()
time.sleep(3*x)
if __name__ == "__main__":
main()
在实际代码中, tasks
的长度远远大于CPU核心的数量,每个task
都是轻量级的,不同的任务占用了大量不同的CPU时间(几分钟到几天)和大量不同的内存(从花生到几个GBS)。 所有这些内存都是run
本地内存,并且不需要共享它 - 所以问题是它是否在run
返回后释放,和/或一旦进程加入。
进程终止时释放进程消耗的内存。 在您的示例中,当run_q()返回时会发生这种情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.