将多个参数传递给concurrent.futures.Executor.map?
concurrent.futures.Executor.map
接受可变数量的可迭代对象,从中调用给定的函数。 如果我有一个生成器,可以生成通常在适当位置解包的元组,我应该如何调用它?
以下内容不起作用,因为每个生成的元组都作为映射的不同参数给出
args = ((a, b) for (a, b) in c)
for result in executor.map(f, *args):
pass
:生成器,映射所需的参数可能如下所示:
executor.map(
f,
(i[0] for i in args),
(i[1] for i in args),
...,
(i[N] for i in args),
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
一个重复的参数,
c
中的一个参数需要解包
c
的项,并且可以解包c
需要解包
的项c
,无法解包c
f
以采用单个参数并解包函数中的参数。如果
c
中的每个项目都有可变数量的成员,或者您仅调用f
几次:它定义了一个新函数,用于从
c
中解压每个项目并调用f
。在lambda
中使用f
的默认参数使f
在lambda
内部成为本地变量,从而减少查找时间。如果您有固定数量的参数,并且需要多次调用
f
:其中
n
是参数的数量f
的参数。这是改编自itertools.tee
。One argument that is repeated, one argument in
c
Need to unpack items of
c
, and can unpackc
Need to unpack items of
c
, can't unpackc
f
to take a single argument and unpack the argument in the function.If each item in
c
has a variable number of members, or you're callingf
only a few times:It defines a new function that unpacks each item from
c
and callsf
. Using a default argument forf
in thelambda
makesf
local inside thelambda
and so reduces lookup time.If you've got a fixed number of arguments, and you need to call
f
a lot of times:Where
n
is the number of arguments tof
. This is adapted fromitertools.tee
.您需要删除
map
调用上的*
:这将调用
f
、len(args)
次,其中f
应该接受一个参数。如果您希望 f 接受两个参数,您可以使用 lambda 调用,例如:
You need to remove the
*
on themap
call:This will call
f
,len(args)
times, wheref
should accept one parameter.If you want
f
to accept two parameters you can use a lambda call like:因此,假设您有一个接受 3 个参数 的函数,并且所有 3 个参数都是动态,并且随着每次调用而不断变化。例如:
要使用线程多次调用此函数,我首先创建一个元组列表,其中每个元组都是 a、b、c 的一个版本:
我们知道
concurrent.futures< /code> 的
map
函数将接受第一个参数作为目标函数,第二个参数作为函数每个版本的参数列表将被执行。因此,您可能会进行如下调用:但这会给您带来错误,即该函数需要
3 个参数,但只得到 1
。为了解决这个问题,我们创建一个辅助函数:现在,我们可以使用执行器调用这个函数,如下所示:
这应该会给你想要的结果。
So suppose you have a function which takes 3 arguments and all the 3 arguments are dynamic and keep on changing with every call. For example:
To call this multiple times using threading, I would first create a list of tuples where each tuple is a version of a,b,c:
To we know that
concurrent.futures
'smap
function would accept first argument as the target function and second argument as the list of arguments for each version of the function that will be execute. Therefore, you might make a call like this:But this will give you error that the function expected
3 arguments but got only 1
. To solve this problem, we create a helper function:Now, we can call this function using executor as follow:
That should give you the desired results.
您可以通过 Python 中的 partial 方法使用柯里化创建新函数
如果您需要传递多个相同的参数,可以将它们传递到 部分方法
You can use currying to create new function via partial method in Python
If you need to pass more than one the same parameters you can pass them to partial method
下面的代码片段展示了如何使用 ThreadPoolExecutor 向函数发送多个参数:
Here's a code snippet showing how to send multiple arguments to a function with ThreadPoolExecutor:
对于
ProcessPoolExecutor.map()< /代码>
:
因此,
ProcessPoolExecutor.map()
的用法与Python内置map()
的用法相同。这是文档:结论:将几个参数传递给
map()
。尝试在 python 3 下运行以下代码片段,你就会很清楚了:
For
ProcessPoolExecutor.map()
:Therefore, the usage of
ProcessPoolExecutor.map()
is the same as that of Python's build-inmap()
. Here is the docs:Conclusion: pass the several parameters to
map()
.Try running the following snippet under python 3, and you will be quite clear:
我在这里看到了很多答案,但没有一个像使用 lambda 表达式那样直接:
foo(x,y):
吗
想要使用相同的值(即 xVal 和 yVal)调用上述方法 10 次 ?
以 concurrent.futures.ThreadPoolExecutor() 作为执行器:
I have seen so many answers here, but none of them is as straight forward as using lambda expressions:
foo(x,y):
pass
want to call above method 10 times, with same value i.e. xVal and yVal?
with concurrent.futures.ThreadPoolExecutor() as executor:
假设您在下面显示的数据框中有这样的数据,并且您希望将第一两列传递给一个函数,该函数将读取图像并预测特征,然后计算差异并返回差异值。
注意:您可以根据您的要求有任何场景,并且可以分别定义函数。
下面的代码片段将这两列作为参数并传递给线程池机制(还显示进度条)
lets say you have data like this in data frame shown below and you want to pass 1st two columns to a function which will read the images and predict the fetaures and then calculate the difference and return the difference value.
Note: you can have any scenario as per your requirement and respectively you can define the function.
The below code snippet will takes these two columns as argument and pass to the Threadpool mechanism (showing the progress bar also)
下面是我一直使用的一个简单实用程序。
示例用法 -
A simple utility that I use all the time is below.
Example usage -