为什么在共享带有字符串的ctypes.Structure而不仅仅是字符串时,子进程的内存使用(python多处理)进程如此不同?

pap*_*ane 7 python memory shared-memory multiprocessing

以下代码使用multiprocessing's Array跨进程共享大量unicode字符串.如果我使用c_wchar_p类型,子进程的内存使用量大约是父进程中使用的内存的四分之一(如果我更改数组中的条目数量,则数量会发生变化).

但是,如果我使用ctypes.Structure单个c_wchar_p字段,则子进程的内存使用量是常量且非常低,而父进程的内存使用量则翻倍.

import ctypes
import multiprocessing
import random
import resource
import time

a = None

class Record(ctypes.Structure):
    _fields_ = [('value', ctypes.c_wchar_p)]
    def __init__(self, value):
        self.value = value

    def __str__(self):
        return '(%s)' % (self.value,)

def child(i):
    while True:
        print "%ik memory used in child %i: %s" % (resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1024, i, a[i])
        time.sleep(1)
        for j in xrange(len(a)):
            c = a[j]

def main():
    global a
    # uncomment this line and comment the next to switch
    #a = multiprocessing.Array(ctypes.c_wchar_p, [u'unicode %r!' % i for i in xrange(1000000)], lock=False)
    a = multiprocessing.Array(Record, [Record(u'unicode %r!' % i) for i in xrange(1000000)], lock=False)
    for i in xrange(5):
        p = multiprocessing.Process(target=child, args=(i + 1,))
        p.start()
    while True:
        print "%ik memory used in parent: %s" % (resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1024, a[0])
        time.sleep(1)

if __name__ == '__main__':
    main()
Run Code Online (Sandbox Code Playgroud)

使用c_wchar_p会产生以下输出:

363224k memory used in parent: unicode 0!
72560k memory used in child 5: unicode 5!
72556k memory used in child 3: unicode 3!
72536k memory used in child 1: unicode 1!
72568k memory used in child 4: unicode 4!
72576k memory used in child 2: unicode 2!
Run Code Online (Sandbox Code Playgroud)

在此输出中使用记录结果:

712508k memory used in parent: (unicode 0!)
1912k memory used in child 1: (unicode 1!)
1908k memory used in child 2: (unicode 2!)
1904k memory used in child 5: (unicode 5!)
1904k memory used in child 4: (unicode 4!)
1908k memory used in child 3: (unicode 3!)
Run Code Online (Sandbox Code Playgroud)

为什么?

Nam*_*yen 2

我不知道内存使用量的增加,但我认为它并没有真正实现您想要做的事情。

如果您a[i]在父进程中进行修改,子进程不会获得相同的值。

最好不要_p在进程之间传递指针(这正是类型的本质)。正如multiprocessing文档中引用的:

尽管可以在共享内存中存储指针,但请记住,这将引用特定进程的地址空间中的位置。但是,该指针很可能在第二个进程的上下文中无效,并且尝试从第二个进程取消引用该指针可能会导致崩溃。