Python 多处理信号灯不起作用



我希望我的程序一次打印出一行,但是它一次打印多行并造成混乱。我似乎无法找出为什么信号量不阻止多个进程相互打印。

如何让它尊重信号量?

这是我的代码的简化版本,在运行时有同样的问题(我在带有Python 2.7.11的Windows上运行(这无法更改)):

import multiprocessing
rightofway = multiprocessing.Semaphore(1)
def writenum(number):
rightofway.acquire()
print("[+] - " + str(number))
rightofway.release()
return
def main():
starting = 0
ending = 50
list = range(starting, ending)
pool = multiprocessing.Pool(10)
pool.map(writenum, list)
return
#Required for Windows multiprocessing
if __name__ == '__main__':
main()

下面是乱码输出的示例:

[+] - 0
[+] - 1
[+] - 2
[+] - 3
[+] - 4
[+] - 5
[+] - 6
[+] - 7
[[+] - 8
+] - 10[
+] - 9[+] - 11
[+] - 12
[[+] - 13+] - 14
[[+] - 15+] - 16
[[+] - 18+] - 17
[[+] - 19+] - 20
[[+] - 22+] - 21
[[+] - 23+] - 24
[[+] - 26+] - 25
[[+] - 27+] - 28
[[+] - 30+] - 29
[[+] - 31+] - 32
[[+] - 34+] - 33
[[+] - 35+] - 36
[[+] - 38+] - 37
[[+] - 39+] - 40
[[+] - 42+] - 41
[[+] - 43+] - 44
[[+] - 46+] - 45
[[+] - 47+] - 48
[+] - 49

这是我想要的输出示例(注意我不关心顺序):

[+] - 0
[+] - 1
[+] - 2
[+] - 3
[+] - 4
[+] - 5
[+] - 6
[+] - 7
[+] - 8
[+] - 9
[+] - 10
[+] - 11
[+] - 12
[+] - 13
[+] - 14
[+] - 15
[+] - 16
[+] - 17
[+] - 18
[+] - 19
[+] - 20
[+] - 21
[+] - 22
[+] - 23
[+] - 24
[+] - 25
[+] - 26
[+] - 27
[+] - 28
[+] - 29
[+] - 30
[+] - 31
[+] - 32
[+] - 33
[+] - 36
[+] - 34
[+] - 35
[+] - 37
[+] - 38
[+] - 40
[+] - 39
[+] - 41
[+] - 42
[+] - 44
[+] - 43
[+] - 45
[+] - 46
[+] - 48
[+] - 47
[+] - 49

你的问题与此类似。

来自多处理编程指南。

将资源显式传递给子进程

。最好将对象作为参数传递给子进程的构造函数。

除了使代码(可能)与Windows兼容之外...

在 Windows 上,需要将共享对象传递给参数的 Process 构造函数列表。否则,子进程将获得全新的副本,而不是父进程的副本。这就是为什么你会觉得Semaphore不起作用。这两个进程正在创建自己不同的Semaphore对象,而不是共享同一个对象。

要将Semaphore对象传递给Windows上的Pool,您需要稍微挣扎一下,但不要太多。由于无法将Semaphore对象直接传递给writenum函数,因此需要依赖Poolinitializer

from multiprocessing import Semaphore, Pool
mutex = None
def initializer(semaphore):
"""This function is run at the Pool startup. 
Use it to set your Semaphore object in the child process.
"""
global mutex
mutex = semaphore
def writenum(args):
with mutex:
print "[+] - " + str(number)
def main():
semaphore = Semaphore()
pool = Pool(initializer=initializer, initargs=[semaphore])
numbers = range(50)
pool.map(writenum, numbers)

编辑:刚刚注意到我写的是Lock而不是Semaphore.核心推理保持不变。

为了让事情变得更容易一些,Follow对我有用。在 Win10 上测试。TL;DR - 使用锁而不是信号量

import multiprocessing
rightofway = multiprocessing.Lock()
def writenum(number):
with rightofway:
print("[+] - " + str(number))
return
def main():
starting = 0
ending = 50
list = range(starting, ending)
pool = multiprocessing.Pool(10)
pool.map(writenum, list)
return
#Required for Windows multiprocessing
if __name__ == '__main__':
main()

相关内容

  • 没有找到相关文章

最新更新