gpt4 book ai didi

python - 在 Python 中使用多处理模块时无法在进程之间互锁

转载 作者:太空宇宙 更新时间:2023-11-03 11:19:57 37 4
gpt4 key购买 nike

我对 Python 有点陌生。我一直在学习和研究多处理模块,但在使用锁定时遇到了这个问题。

测试代码如下:

import multiprocessing
from multiprocessing import Pool

class Param_Class():

#Initialisation.
def __init__(self):
self.counter = multiprocessing.Value('i',0)
self.lock = multiprocessing.Lock()

def inc_counter(self):
lk.acquire()
print('Locked {}'.format(multiprocessing.current_process().name))
self.counter.value+=1
lk.release()
print('Released {}'.format(multiprocessing.current_process().name))

param_class = Param_Class()

def init(lock):
global lk
lk = lock


def target():
param_class.inc_counter()
code = 0
return code

def handler(i):
if i<50:
code = target()
print(param_class.counter.value)


if __name__ == '__main__':
p = Pool(processes=2,initializer=init, initargs=(param_class.lock,))
p.map_async(handler,range(50))
p.close()
p.join()

我期望的是每个进程在另一个进程执行时被锁定,因此每次执行其中一个进程时我的计数器应该递增。但是,进程之间没有发生互锁,第二个进程正在处理计数器的副本。

这是输出:

Locked SpawnPoolWorker-1
Released SpawnPoolWorker-1
1
Locked SpawnPoolWorker-1
Released SpawnPoolWorker-1
Locked SpawnPoolWorker-2
2
Released SpawnPoolWorker-2
1
Locked SpawnPoolWorker-1
Locked SpawnPoolWorker-2
Released SpawnPoolWorker-1
Released SpawnPoolWorker-2
3
2
Locked SpawnPoolWorker-1
Released SpawnPoolWorker-1
Locked SpawnPoolWorker-2
4
Released SpawnPoolWorker-2
Locked SpawnPoolWorker-1
3
Released SpawnPoolWorker-1
Locked SpawnPoolWorker-2
5
Released SpawnPoolWorker-2
Locked SpawnPoolWorker-1
4
Released SpawnPoolWorker-1
Locked SpawnPoolWorker-2
6
Released SpawnPoolWorker-2
Locked SpawnPoolWorker-1
5
Released SpawnPoolWorker-1
Locked SpawnPoolWorker-2
7
Released SpawnPoolWorker-2
Locked SpawnPoolWorker-1
6
Released SpawnPoolWorker-1
Locked SpawnPoolWorker-2
8
Released SpawnPoolWorker-2
Locked SpawnPoolWorker-1
7
Released SpawnPoolWorker-1
Locked SpawnPoolWorker-2
9
Released SpawnPoolWorker-2
Locked SpawnPoolWorker-1
8
Released SpawnPoolWorker-1
Locked SpawnPoolWorker-2
10
Released SpawnPoolWorker-2
Locked SpawnPoolWorker-1
9
Released SpawnPoolWorker-1
Locked SpawnPoolWorker-2
11
Released SpawnPoolWorker-2
10
Locked SpawnPoolWorker-1
Locked SpawnPoolWorker-2
Released SpawnPoolWorker-1
Released SpawnPoolWorker-2
12
11
Locked SpawnPoolWorker-1
Released SpawnPoolWorker-1
Locked SpawnPoolWorker-2
13
Released SpawnPoolWorker-2
12
Locked SpawnPoolWorker-1
Released SpawnPoolWorker-1
Locked SpawnPoolWorker-2
14
Released SpawnPoolWorker-2
13
Locked SpawnPoolWorker-1
Released SpawnPoolWorker-1
Locked SpawnPoolWorker-2
15
Released SpawnPoolWorker-2
Locked SpawnPoolWorker-1
14
Released SpawnPoolWorker-1
Locked SpawnPoolWorker-2
16
Released SpawnPoolWorker-2
Locked SpawnPoolWorker-1
15
Released SpawnPoolWorker-1
Locked SpawnPoolWorker-2
17
Released SpawnPoolWorker-2
Locked SpawnPoolWorker-1
16
Released SpawnPoolWorker-1
Locked SpawnPoolWorker-2
18
Released SpawnPoolWorker-2
Locked SpawnPoolWorker-1
17
Released SpawnPoolWorker-1
Locked SpawnPoolWorker-2
19
Released SpawnPoolWorker-2
Locked SpawnPoolWorker-1
18
Released SpawnPoolWorker-1
Locked SpawnPoolWorker-2
20
Released SpawnPoolWorker-2
Locked SpawnPoolWorker-1
19
Released SpawnPoolWorker-1
Locked SpawnPoolWorker-2
21
Released SpawnPoolWorker-2
Locked SpawnPoolWorker-1
20
Released SpawnPoolWorker-1
Locked SpawnPoolWorker-2
22
Released SpawnPoolWorker-2
Locked SpawnPoolWorker-1
21
Released SpawnPoolWorker-1
Locked SpawnPoolWorker-2
23
Released SpawnPoolWorker-2
Locked SpawnPoolWorker-1
22
Released SpawnPoolWorker-1
24
Locked SpawnPoolWorker-1
Released SpawnPoolWorker-1
25
Locked SpawnPoolWorker-1
Released SpawnPoolWorker-1
26
Locked SpawnPoolWorker-1
Released SpawnPoolWorker-1
27
Locked SpawnPoolWorker-1
Released SpawnPoolWorker-1
28

期望的输出应该是这样的:

Locked SpawnPoolWorker-1
1
Released SpawnPoolWorker-1
Locked SpawnPoolWorker-2
2
Released SpawnPoolWorker-2
Locked SpawnPoolWorker-1
3
Released SpawnPoolWorker-1
.
.
.
.
.
.
.
Locked SpawnPoolWorker-2
50
Released SpawnPoolWorker-2

我提到了 this了解如何初始化锁并继承它。我无法理解我在哪里搞砸了。是锁还是 multiprocessing.Pool 调用或使用类实例或目标函数本身。请用一些理论来详细说明它以支持它。我想了解这个,而不仅仅是解决它。

请注意,在获得的输出中,它如何按预期从值 24 到 28 工作,并且进程 2 刚刚停止工作。也需要澄清这一点。

P.S.:我想避免使用管理器,因为它是链接答案中提到的重量级。此外,我不想消除执行的部分并发,因为我将使用不需要锁定的其他变量,并且我希望模块保持快速和 Pythonic。

最佳答案

显然,每次进程进行应用时,都会调用类的init,从而为每个进程创建一个副本。

因此,为了克服这个问题,与其将计数器创建为类属性,不如将其传递到池的 init 中并使其成为全局的。

此外,锁是微不足道的。

关于python - 在 Python 中使用多处理模块时无法在进程之间互锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44983660/

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