单元测试中多处理模块的酸洗错误,但不是通过常规python。

时间:2017-07-20 19:04:27

标签: python python-2.7 python-multiprocessing

在使用多处理程序包时,我在单元测试中遇到此错误。
    PicklingError: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed

运行以下代码在常规python文件中运行,但是当放入 unittest 时,它会引发上面发布的酸洗错误。这是我可以提出的最简单的例子。

    # The following, works. 
    import multiprocessing

    def hello(number):
        print "hello"

    number_processes = 2
    pool = multiprocessing.Pool(number_processes)
    total_tasks = 2
    tasks = range(total_tasks)
    results = pool.map(hello, tasks)
    pool.close()
    pool.join()

下一个代码块:

     # This does not work, i'm running it via unittest runner
    import multiprocessing
    import unittest
    class Testing123(unittest.TestCase):
        test_1(self):

            def hello(number):
                print "hello"

            number_processes = 2
            pool = multiprocessing.Pool(number_processes)
            total_tasks = 2
            tasks = range(total_tasks)
            results = pool.map(hello, tasks)
            pool.close()
            pool.join()

0 个答案:

没有答案