使用类函数将多个参数传递给pool.map [英] Passing multiple arguments to pool.map using class function
问题描述
我正在尝试按照这篇文章中所述进行线程化,并通过工作在Python 2.7中传递多个参数-此处中所述.
I'm trying to thread as described in this post, and also pass multiple arguments in Python 2.7 through a work-around described here.
现在我有类似这样的功能,它是类pair_scraper
的一部分:
Right now I have something like this, a function that is part of class pair_scraper
:
def pool_threading(self):
pool = ThreadPool(4)
for username in self.username_list:
master_list = pool.map(self.length_scraper2,
itertools.izip(username*len(self.repo_list),
itertools.repeat(self.repo_list)))
def length_scraper2(self, username, repo):
#code
但是,当我运行代码时,出现错误消息:
However, when I run my code I get the error:
TypeError: length_scraper2() takes exactly 3 arguments (2 given)
这似乎是因为它希望将self
作为参数传递,考虑到我在类中使用类函数,这是没有意义的.关于如何解决的想法?
Which seems to be because it wants self
passed as an argument, which is nonsensical given I'm using a class function within the class. Thoughts on how to fix?
推荐答案
itertools.izip(username*len(self.repo_list),itertools.repeat(self.repo_list))
产生tuple
.
您需要显式地将2个参数传递给您的方法(self
被隐式传递,因为它是一个非静态方法),但是您仅显式地传递1个元组,加上隐式的self
可以传递2个参数,因此令人困惑的错误消息.
You need to pass 2 arguments explicitly to your method (self
is implicitly passed because it's a non-static method), but you only pass 1 tuple explicitly, plus the implicit self
which makes 2 arguments, hence the confusing error message.
您必须使用*
将元组作为2个单独的参数传递,如下所示:
You have to use *
to pass your tuple as 2 separate arguments, like this:
master_list = pool.map(self.length_scraper2,
*itertools.izip(username*len(self.repo_list),itertools.repeat(self.repo_list)))
使用经典map
在简单函数上进行的简单测试:
simple test using the classical map
on a simple function:
def function(b,c):
return (b,c)
print(list(map(function,zip([1,2],[4,5]))))
错误:
print(list(map(function,zip([1,2],[4,5]))))
TypeError: function() missing 1 required positional argument: 'c'
现在添加单个星号以扩展args:
now adding single asterisk to expand args:
print(list(map(function,*zip([1,2],[4,5]))))
有效:
[(1, 2), (4, 5)]
类方法相同:
class Foo:
def function(self,b,c):
return (b,c)
f = Foo()
print(list(map(f.function,*zip([1,2],[4,5]))))
这篇关于使用类函数将多个参数传递给pool.map的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!