This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Michael.Olson
Recipients Michael.Olson
Date 2010-10-02.14:00:27
SpamBayes Score 8.105007e-06
Marked as misclassified No
Message-id <1286028029.62.0.429525925265.issue10015@psf.upfronthosting.co.za>
In-reply-to
Content
Using Python 2.7 x32 on Windows XP

Attempting to create a multiprocessing.pool.ThreadPool
in a child thread created using threading.Thread, an
AttributeError is thrown. A ThreadPool created in the 
main thread can be passed to the child thread and used.


Exact text of exception
-------------------
  File "D:\Dev\Python27\lib\multiprocessing\dummy\__init__.py", line 47, in star
t
    self._parent._children[self] = None
AttributeError: 'Thread' object has no attribute '_children'


Demonstration Code
-------------------
import unittest
from threading import Thread
from multiprocessing.pool import ThreadPool


def f(x):
    return x*x


def create_and_run(cb, pool = None):
    if not pool:
        pool = ThreadPool(2)
    r = pool.map_async(f, range(10))
    cb(r.get())


class TestThreadPool(unittest.TestCase):
    def setUp(self):
        self.expected = [f(x) for x in range(10)]

    def callback(self, data):
        self.data = data

    def test_creating_pool_in_mainthread(self):
        """Test multiprocessing.pool.ThreadPool from main thread"""
        self.data = None
        create_and_run(self.callback)
        self.assertEqual(self.data, self.expected)

    def test_creating_pool_in_subthread(self):
        """Test multiprocessing.pool.ThreadPool from a child thread."""
        self.data = None
        t = Thread(target=create_and_run, args=[self.callback])
        t.start()
        t.join()
        self.assertEqual(self.data, self.expected)

    def test_creating_pool_in_subthread_workaround(self):
        """Test running a ThreadPool created in main thread, used in child."""
        self.data = None
        pool = ThreadPool(2)
        t = Thread(target=create_and_run, args=[self.callback, pool])
        t.start()
        t.join()
        self.assertEqual(self.data, self.expected)


if __name__ =='__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(TestThreadPool)
    unittest.TextTestRunner(verbosity=2).run(suite)
History
Date User Action Args
2010-10-02 14:00:29Michael.Olsonsetrecipients: + Michael.Olson
2010-10-02 14:00:29Michael.Olsonsetmessageid: <1286028029.62.0.429525925265.issue10015@psf.upfronthosting.co.za>
2010-10-02 14:00:28Michael.Olsonlinkissue10015 messages
2010-10-02 14:00:27Michael.Olsoncreate