Python多处理信号量不工作

时间:2018-05-16 19:58:03

标签: python multithreading multiprocessing semaphore

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

我怎样才能让它尊重信号量?

以下是我的代码的简化版本,在运行时遇到同样的问题(我在Windows上使用Python 2.7.11运行(无法更改)):

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

2 个答案:

答案 0 :(得分:2)

您的问题类似于this one

来自多处理编程指南。

  

明确地将资源传递给子进程

     

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

     

除了使代码(可能)与 Windows ...

兼容

在Windows上,您需要将共享对象传递给Process构造函数参数列表。否则,子进程将获得一个全新的副本而不是父进程。这就是为什么你得到Semaphore没有效果的原因。这两个进程正在创建自己独特的Semaphore对象,而不是共享同一个对象。

要在Windows上将Semaphore对象传递给Pool,您需要努力但不要太多。由于您无法直接将Semaphore对象传递给writenum函数,因此您需要依赖Pool initializer

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。核心推理保持不变。

答案 1 :(得分:0)

为了让事情变得更轻松,以下为我工作。在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()