• (摘抄) 源码分析multiprocessing的Value Array共享内存原理


    原文地址:

    http://xiaorui.cc/archives/3290

    ============================================================

    摘抄内容:

    接着粗略的聊聊multiprocessing共享内存的实现方法.

    multiprocessing提前设定的ctypes映射表,这样对你来说只需要传递符号就可以了。 

    typecode_to_type = {
        'c': ctypes.c_char,  'u': ctypes.c_wchar,
        'b': ctypes.c_byte,  'B': ctypes.c_ubyte,
        'h': ctypes.c_short, 'H': ctypes.c_ushort,
        'i': ctypes.c_int,   'I': ctypes.c_uint,
        'l': ctypes.c_long,  'L': ctypes.c_ulong,
        'f': ctypes.c_float, 'd': ctypes.c_double
        }

    下面的代码通过mmap ctypes实现了一个简单的数据共享, 这样对于我们来说,可以像操作python对象那样操作映射的内存地址

    (经过修改后的Python3.7代码)

    ###  a.py

    import ctypes
    import mmap
    import os
    import struct
    import time
    
    
    def main():
        fd = os.open('/tmp/mmaptest', os.O_CREAT | os.O_TRUNC | os.O_RDWR)
    
        assert os.write(fd, b'x00' * mmap.PAGESIZE) == mmap.PAGESIZE
    
        buf = mmap.mmap(fd, 0, mmap.MAP_SHARED, mmap.PROT_WRITE)
    
        i = ctypes.c_int.from_buffer(buf)
    
        # Before we create a new value, we need to find the offset of the next free
        # memory address within the mmap
        # print(offset, ctypes.sizeof(i))  # byte length
        offset = struct.calcsize(i._type_)
    
        # The offset should be uninitialized ('x00')
        #assert buf[offset] == b'x00'
        #print(type(buf[offset]))
        assert buf[offset] == 0x00
    
        # Now ceate a string containing 'foo' by first creating a c_char array
        s_type = ctypes.c_char * len('foot')
    
        # Now create the ctypes instance
        s = s_type.from_buffer(buf, offset)
    
    
        s.raw = b'foot'
        i.value = 10
    
    
        i.value *= i.value
        print( 'Changing i : {}'.format(i.value) )
    
        s.raw = b'bara'
        print( 'Changing s : {}'.format(s.raw) )
    
    
    if __name__ == '__main__':
        main()
    
        time.sleep(60)

    运行结果:

    ### b.py

    import mmap
    import os
    import struct
    import time
    
    def main():
        # Open the file for reading
        fd = os.open('/tmp/mmaptest', os.O_RDONLY)
    
        # Memory map the file
        buf = mmap.mmap(fd, 0, mmap.MAP_SHARED, mmap.PROT_READ)
    
        i = None
        s = None
    
        new_i, = struct.unpack('i', buf[:4])
        new_s, = struct.unpack('4s', buf[4:8])
    
        if i != new_i or s != new_s:
            print( 'i: %s => %d' % (i, new_i) )
            print( 's: %s => %s' % (s, new_s) )
            i = new_i
            s = new_s
    
    if __name__ == '__main__':
        main()

    运行结果:

    本博客是博主个人学习时的一些记录,不保证是为原创,个别文章加入了转载的源地址还有个别文章是汇总网上多份资料所成,在这之中也必有疏漏未加标注者,如有侵权请与博主联系。
  • 相关阅读:
    PDF数据提取------2.相关类介绍
    Google搜索的常用技巧
    a helper class for generating xls or csv file
    正则 提取html标签value
    获取 windows地址栏 网页地址栏 文件名
    MSSQL将逗号分隔的字符串转换成列显示
    C# String.Format字符串中包含"{" "}"时需注意的问题
    格式化JSON中时间
    Oracle 10G创建表空间
    Sqlserver取最近一分组中最新一条记录
  • 原文地址:https://www.cnblogs.com/devilmaycry812839668/p/15038579.html
Copyright © 2020-2023  润新知