gpt4 book ai didi

python - 在 python : About several instances with same name at the same time 中使用池进行多处理

转载 作者:太空狗 更新时间:2023-10-30 02:18:02 25 4
gpt4 key购买 nike

我对多处理有点陌生。但是,假设我们有如下程序。该程序似乎运行良好。现在的问题。在我看来,我们将同时拥有 4 个同名 (a) 的 SomeKindOfClass 实例。这怎么可能?此外,这种编程是否存在潜在风险?

from multiprocessing.dummy import Pool
import numpy
from theFile import someKindOfClass

n = 8
allOutputs = numpy.zeros(n)

def work(index):
a = SomeKindOfClass()
a.theSlowFunction()
allOutputs[index] = a.output

pool = Pool(processes=4)
pool.map(work,range(0,n))

最佳答案

名称 a 仅在您的 work 函数范围内是局部的,因此这里没有名称冲突。在内部,python 将使用唯一标识符跟踪每个类实例。如果你想检查这个,你可以使用 id 函数检查对象 ID:

print(id(a))

我没有发现您的代码有任何问题。

关于python - 在 python : About several instances with same name at the same time 中使用池进行多处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37838789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com