内置的功能ID在多处理中的行为

时间:2019-11-11 03:26:01

标签: python cpython

import multiprocessing
import random
import time
import uuid


class Test(object):
    def __init__(self):
        self.value = uuid.uuid4()
        self.value2 = uuid.uuid1()


def print_id():
    time.sleep(3)
    t = Test()
    print(time.time(), multiprocessing.current_process().name, id(t.value), t.value)
    time.sleep(3)


if __name__ == '__main__':
    for i in range(20):
        multiprocessing.Process(target=print_id).start()

在python文档中,id函数被注释为“ CPython实现细节:这是对象在内存中的地址。”

如果id指向内存中对象的地址,我希望id(t.value)具有不同的值。 (为避免整数池或字符串池的影响,我使用uuid生成随机字符串)。

但是,结果如下所示在Linux(windows子系统)中运行

1573442491.7650847 Process-5 139809379339728 e26051eb-1a5e-45b2-a279-8a87d902fb5f
1573442491.76799 Process-6 139809377286352 be08771f-0592-4c6c-a17a-778f869964d1
1573442491.7683392 Process-7 139809377286352 f1281fe8-b66b-46d8-826f-1a1587d96385
1573442491.7693052 Process-10 139809377286352 6d594352-6606-4bb0-bd4f-3ff88c775fa8
1573442491.7711256 Process-8 139809377286352 0d6d1aa5-fd03-4d1f-98e8-38973aebaacb
1573442491.7724354 Process-1 139809379338768 d4978687-fb5f-4d10-bbee-bcba34229829
1573442491.772816 Process-2 139809379336848 7d264c1a-5634-4798-891d-4eb0e738079e
1573442491.7740486 Process-3 139809377286480 efa39c48-e92a-4feb-b6fc-c648485dce1f
1573442491.7740998 Process-4 139809379338896 6957c2d0-e9fa-4be1-b9fb-318f2bed51f4
1573442491.7750933 Process-9 139809377286352 acc3129e-8dfa-48a5-a652-60eb71068f07
1573442492.0212004 Process-12 139809377286352 efc229d7-abb3-4101-a73d-cacb1098a4d0
1573442492.0212717 Process-11 139809377286352 2733532b-77cc-46ff-99da-c87738711cb1
1573442492.0219014 Process-13 139809377286352 19de43bb-efc1-426b-9724-5f3546101fa3
1573442492.0264502 Process-17 139809377286352 9d894bac-c60e-40be-931d-110d08c1ed57
1573442492.02747 Process-15 139809377286352 ffb3a1d1-eb11-48d3-a2dd-d460452ee2c7
1573442492.0276544 Process-14 139809377286352 0c9a683c-8de1-46c1-8638-ffb033cef588
1573442492.028296 Process-19 139809377286352 1949a743-3092-467d-b64a-8d3f2d315c89
1573442492.0284011 Process-16 139809377286352 00c0ec39-b2e2-40bc-a3e2-0de2362c9d60
1573442492.0284972 Process-18 139809377286352 3efbf7f5-b9fa-40d4-a1d5-b4a21f462e75
1573442492.0286424 Process-20 139809377286352 cea3d24b-73e8-43e5-a121-c37995c1b029

可以注意到,某些进程将返回具有相同id(t.value)和不同t.value的

id函数真的返回内存的真实地址吗? 如果没有,python或cpython中内存地址映射的隐藏规则是什么? 我应该如何解释输出?

0 个答案:

没有答案