如何解决在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 举报,一经查实,本站将立刻删除。