多处理:池和映射以及sys.exit()

时间:2019-06-21 07:30:59

标签: python multiprocessing

我认为我需要一些建议。下面是我的代码:

from multiprocessing import Pool
import time
import sys

def testing(number):
    count = 0
    while True:
        print('Count: {}'.format(count))
        count += 1

        if count > number:
            print('Exiting...')
            sys.exit()
        else:
            print('Looping Over')
            time.sleep(1)

if __name__ == '__main__':

    with Pool(2) as p:
        p.map(testing, [3, 2])

预期结果:

一旦所有子线程都退出,程序(主线程)应退出。

实际结果:

$ python3 test_exit.py
Count: 0
Looping Over
Count: 0
Looping Over
Count: 1
Looping Over
Count: 1
Looping Over
Count: 2
Looping Over
Count: 2
Exiting...   <<< Exited 1st thread.
Count: 3
Exiting...   <<< Exited 2nd thread.
....and it stays here as if stuck or something. It never gives control back to Shell.

预期结果:

$ python3 test_exit.py
Count: 0
Looping Over
Count: 0
Looping Over
Count: 1
Looping Over
Count: 1
Looping Over
Count: 2
Looping Over
Count: 2
Exiting...
Count: 3
Exiting...
$   <<< Note: I am expecting to be dropped back to Shell prompt

问题:

就池/地图使用而言,我的方法是否存在问题?

3 个答案:

答案 0 :(得分:1)

一旦所有子线程都退出,程序(主线程)应退出。

  • 通过终止其目标函数testing()(在关键循环中通过break语句完成)来完成一个过程
  • 在进程池完成后退出主线程/程序。

from multiprocessing import Pool, current_process
import time
import sys

def testing(number):
    count = 0
    while True:
        print('Count: {}'.format(count))
        count += 1

        if count > number:
            print('Exiting...', current_process().name)
            break
        else:
            print('Looping Over')
            time.sleep(1)

if __name__ == '__main__':

    with Pool(2) as p:
        p.map(testing, [3, 2])
    sys.exit()

输出:

Count: 0
Looping Over
Count: 0
Looping Over
Count: 1
Looping Over
Count: 1
Looping Over
Count: 2
Looping Over
Count: 2
Exiting... ForkPoolWorker-2
Count: 3
Exiting... ForkPoolWorker-1
$

答案 1 :(得分:1)

对此行为的解释:

之所以会发生这种情况,是因为当您调用sys.exit()时会引发systemExit异常。由于sys.exit()最终仅会引发异常,它只会退出以下过程:被调用,并且不会进一步传播到主要进程。

一旦所有子进程退出,主进程就坐在那里等待从子进程返回某些内容。所有子进程都已经退出,因此没有任何返回可导致永远的等待

答案 2 :(得分:0)

尝试使用os._exit(1)而不是sys.exit();