我有一个大型的只读bytes
对象,我需要在几个不同的Python(3)进程中进行操作,每个进程“返回”(添加到结果队列)基于结果的列表他们的工作。
由于此对象非常大且只读,因此我希望避免将其复制到每个工作进程的地址空间中。我所做的研究表明,共享内存是解决这个问题的正确方法,但我无法找到一个很好的资源/示例,说明如何使用multiprocessing
模块完成此操作。
提前致谢。
答案 0 :(得分:2)
如果ctypes
类型为# No lock needed, as no write will be done.
array = multiprocessing.Array(ctypes.c_char, long_byte_string, lock=False)
,您可以使用multiprocessing.Array
,例如ctypes.Array
,但对于共享内存。
>>> import multiprocessing
>>> import ctypes
>>> array = multiprocessing.Array(ctypes.c_char, b'\x01\x02\xff\xfe', lock=False)
>>> array[0]
b'\x01'
>>> array[2:]
b'\xff\xfe'
>>> array[:]
b'\x01\x02\xff\xfe'
>>> b'\xff' in array
True
例如:
┌------------┐
| 1 | 1 is the ViewPager fragment
| ┌---------┐|
| | 2 || 2 is the fragment inside ViewPager fragment
| |┌-------┐||
| ||3 ||| 3 is the sub fragment containing the ScrollView with EditText form
| ||form |||
| ||here |||
| || |||
| |└-------┘||
| └---------┘|
└------------┘