Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
unittest中多处理模块的酸洗错误,但不是通过常规python。_Python_Python 2.7_Python Multiprocessing - Fatal编程技术网

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

unittest中多处理模块的酸洗错误,但不是通过常规python。,python,python-2.7,python-multiprocessing,Python,Python 2.7,Python Multiprocessing,在使用多处理程序包时,我在unittest中遇到此错误。 PicklingError:无法pickle:属性查找\uuuu内置\uuuu。函数失败 运行以下代码可以在常规python文件中工作,但是当放入unittest时,它会引发pickle错误,正如我在上面发布的那样。这是我能想出的最简单的例子 # The following, works. import multiprocessing def hello(number): print "hel

在使用多处理程序包时,我在unittest中遇到此错误。
PicklingError:无法pickle:属性查找\uuuu内置\uuuu。函数失败

运行以下代码可以在常规python文件中工作,但是当放入unittest时,它会引发pickle错误,正如我在上面发布的那样。这是我能想出的最简单的例子

    # 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()

看看答案。基本上(令人沮丧的是),多处理只能pickle位于模块顶层的函数。我在这里读到Tim Peters的回答,他说他们很难实现某种修复来完成您想做的事情。非常感谢,我现在正在解决这个问题,没有使用python闭包,只是将函数作为py文件中的第一个函数。