Pickling error with multiprocessing module in a unittest, but not via regular python.

Viewed 1278

I am getting this error inside a unittest while using the multiprocessing package.
PicklingError: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed

Running the following code works inside of a regular python file but when put into a unittest, it raises the pickling error as I posted above. This is the simplest example I could spin up.

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

Next code block:

     # 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 Answers
Related