multiprocess.Pool和classmethod如何在python3.4的unittest中工作

时间:2014-06-30 04:03:43

标签: multiprocessing python-3.3 python-unittest python-3.4

我正在尝试为Lock对象编写unittest,比如

import time
import unittest
from multiprocessing import Pool


class LockTestCase(unittest.TestCase):

    def f(self, x):
        time.sleep(1)

    def test_lock(self):
        p = Pool(2)
        p.map(self.f, (1, 1, 1))

unittest.main()

让我们在TestCase类中保留f,它适用于python3.3,但是当使用python3.4时,它就出来了

File "/usr/lib/python3.4/multiprocessing/pool.py", line 260, in map
  return self._map_async(func, iterable, mapstar, chunksize).get()
File "/usr/lib/python3.4/multiprocessing/pool.py", line 599, in get
  raise self._value
File "/usr/lib/python3.4/multiprocessing/pool.py", line 383, in _handle_tasks
  put(task)
File "/usr/lib/python3.4/multiprocessing/connection.py", line 206, in send
  self._send_bytes(ForkingPickler.dumps(obj))
File "/usr/lib/python3.4/multiprocessing/reduction.py", line 50, in dumps
  cls(buf, protocol).dump(obj)
TypeError: cannot serialize '_io.TextIOWrapper' object

那么,Pool中的classmethod f如何在python3.4中运行?

感谢

1 个答案:

答案 0 :(得分:0)

我的解决方案是在LockTestCase之外定义函数'f'。也许这是最简单的。但是你运行'f'无论如何只运行'map'方法。