gpt4 book ai didi

Python:删除共享变量并清理每个循环中变量使用的共享内存

转载 作者:行者123 更新时间:2023-12-05 07:47:38 26 4
gpt4 key购买 nike

在 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()

最佳答案

您需要在使用共享内存缓冲区的每个实例上调用 close()(在您的情况下可能只有一个),然后仅调用一次 unlink() 以释放那个内存。请参阅此处的文档:

https://docs.python.org/3/library/multiprocessing.shared_memory.html#multiprocessing.shared_memory.SharedMemory.unlink

关于Python:删除共享变量并清理每个循环中变量使用的共享内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39586280/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com