pool.map在python中完成它的工作后是否可以执行代码?

时间:2014-09-10 20:12:49

标签: python pool

我想在pool.map函数调用中收集时间和失败消息,并在完成所有作业后打印它们。这在python中可能吗?

也许有一些事件......或其他......

failures = []

def test():
   sleep(1)
   global failures
   failures.append('test message')

def main():
    data = [...]
    pool = ThreadPool(45)
    results = pool.map(test, data)
    pool.close()
    pool.join()

    # after all calls are finished i want to perform this
    print failures

1 个答案:

答案 0 :(得分:2)

示例(请注意,您可以返回任何内容,返回 [not raise]异常而不是正常结果):

import random
import pprint
import multiprocessing

def test(value):
    if random.random() > 0.5:
        return 'Noes, we got an error for %d!' % value
    else:
        return 'All is ok for %d :)' % value

def main():
    data = range(10)

    pool = multiprocessing.Pool()
    results = pool.map(test, data)
    pool.close()
    pool.join()

    pprint.pprint(results)

if __name__ == '__main__':
    main()

示例输出:

['All is ok for 0 :)',
 'Noes, we got an error for 1!',
 'Noes, we got an error for 2!',
 'Noes, we got an error for 3!',
 'All is ok for 4 :)',
 'Noes, we got an error for 5!',
 'All is ok for 6 :)',
 'Noes, we got an error for 7!',
 'All is ok for 8 :)',
 'Noes, we got an error for 9!']