在python中使用multiprocessing.Array时如何清理空闲内存?

问题描述

我正在使用multiprocessing.Array()与我的父级和子级进程(由multiprocessing.Process()启动)创建共享内存。我想free() Array()分配给ctypes array的内存。我该怎么办?

请考虑以下示例python脚本示例,该脚本定义了一个简单的helloWorld()函数,该函数在共享内存中使用字节数组并存储“ hello world!”。

#!/usr/bin/env python3
import multiprocessing

# this function will be executed in a child process asynchronously
def helloWorld( shm_string ):
    shm_string.value =  bytes( 'hello world!','utf-8' )

# alloc()
shm_string = multiprocessing.Array( 'c',256 )

# execute the helloWorld() function in a child process in the background
process = multiprocessing.Process(
 target = helloWorld,args = (shm_string,),)
process.start()

# <this is where async stuff would happen>

# wait for the process to finish
# and free() the resources allocated for the Process()
process.join()

# get the string out of shared memory
result = shm_string.value.decode( 'utf-8' )
print(result)

# free()
#free(shm_string)??

如上所述,函数helloWorld()在子进程中使用multiprocessing.Process()执行,并且已将字节数组传递到共享内存中。

以下是执行示例:

user@host:~$ python3 example3.py 
hello world!
user@host:~$ 

在我们不再需要上面的脚本时,如何将其更新为实际上free()分配给字节数组(由shm_string引用)的内存?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...