Python Multiprocessing: AttributeError: 'Test' object has no attribute 'get_type'

Python Multiprocessing: AttributeError: 'Test' object has no attribute 'get_type'

简短版本:

我在并行化使用实例方法的代码时遇到问题。

更长的版本:

此 python 代码产生错误:

Error
Traceback (most recent call last):
  File "/Users/gilzellner/dev/git/3.2.1-build/cloudify-system-tests/cosmo_tester/test_suites/stress_test_openstack/test_file.py", line 24, in test
self.pool.map(self.f, [self, url])
File "/Users/gilzellner/.virtualenvs/3.2.1-build/lib/python2.7/site-packages/pathos/multiprocessing.py", line 131, in map
return _pool.map(star(f), zip(*args)) # chunksize
File "/Users/gilzellner/.virtualenvs/3.2.1-build/lib/python2.7/site-packages/multiprocess/pool.py", line 251, in map
return self.map_async(func, iterable, chunksize).get()
File "/Users/gilzellner/.virtualenvs/3.2.1-build/lib/python2.7/site-packages/multiprocess/pool.py", line 567, in get
raise self._value
AttributeError: 'Test' object has no attribute 'get_type'

这是我遇到的一个真实问题的简化版。

import urllib2
from time import sleep
from os import getpid
import unittest
from pathos.multiprocessing import ProcessingPool as Pool

class Test(unittest.TestCase):

    def f(self, x):
        print urllib2.urlopen(x).read()
        print getpid()
        return

    def g(self, y, z):
        print y
        print z
        return

    def test(self):
        url = "http://nba.com"
        self.pool = Pool(processes=1)
        for x in range(0, 3):
            self.pool.map(self.f, [self, url])
            self.pool.map(self.g, [self, url, 1])
        sleep(10)

由于此处的推荐,我正在使用 pathos.multiprocessing: Multiprocessing: Pool and pickle Error -- Pickling Error: Can't pickle <type 'instancemethod'>: attribute lookup __builtin__.instancemethod failed

在使用 pathos.multiprocessing 之前,错误是:

"PicklingError: Can't pickle <type 'instancemethod'>: attribute lookup __builtin__.instancemethod failed"

该错误表明您正在尝试读取未为对象测试定义的属性。

AttributeError: 'Test' 对象没有属性 'get_type'"

在您的 class 测试中,您没有定义 get_type 方法或任何其他属性,因此出现错误。

您错误地使用了多重处理 map 方法。
根据python docs:

A parallel equivalent of the map() built-in function (it supports only one iterable argument though).

凡标准map:

Apply function to every item of iterable and return a list of the results.

Example usage:

from multiprocessing import Pool

def f(x):
    return x*x

if __name__ == '__main__':
    p = Pool(5)
    print(p.map(f, [1, 2, 3]))

您要查找的是apply_async方法:

def test(self):
    url = "http://nba.com"
    self.pool = Pool(processes=1)
    for x in range(0, 3):
        self.pool.apply_async(self.f, args=(self, url))
        self.pool.apply_async(self.g, args=(self, url, 1))
    sleep(10)