繁体   English   中英

Python多处理:AttributeError:'Test'对象没有属性'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: 多处理:池和pickle错误 - 酸洗错误:无法pickle < type'instancemethod '>:属性查找__builtin __。instancemethod failed

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

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

您错误地使用了多处理map方法。
根据python文档

map()内置函数的并行等价物(它只支持一个可迭代的参数)。

凡标准map

将函数应用于iterable的每个项目并返回结果列表。

用法示例

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)

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

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

在类测试中,您尚未定义get_type方法或任何其他属性,因此错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM