在python中,我试图创建一个可由多个处理器访问的共享变量。这项工作将多次循环。我想删除共享变量并清理每个循环中变量使用的共享内存。有人可以帮忙吗?
Ubuntu 14.04 64位
Python 2.7.6
Numpy 1.11.1
import numpy as np
import multiprocessing as mp
import ctypes
import os
def parallel_doSomeWork(train_data):
#...
#...
#...
train_data[0, 0 ,0] = 1.0
def main():
for n in range(10):
# print memory usage info, the shared memory is supposed to be zero here
os.system("free -g")
# creating shared varibale train_data, that can be accessed by multiprocessors
train_data_s = mp.Array(ctypes.c_float, 160000 * 64 * 64)
train_data = np.frombuffer(train_data_s.get_obj(), dtype=np.float32).reshape(160000, 64, 64, 1)
# print memory usage info, the shared memory is supposed to be used here
parallel_doSomeWork(train_data)
# how to delete shared variables and clean the shared memory used by the variables in each loop?
# To make shared = 0 at next print
#.......
#.......
#.......
del train_data_s, train_data
# print memory usage info, the shared memory is supposed to be zero here
os.system("free -g")
if __name__ == '__main__':
main()
答案 0 :(得分:0)
您需要在每个实例上使用共享内存缓冲区(在您的情况下可能只有一个)调用close()
,然后仅调用unlink()
一次以释放该内存。在此处查看文档: