- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
您好 stackoverflow 用户,
我试图查找这一点,但找不到答案:我本质上喜欢并行处理一个函数(独立进程!)并且该函数有一个可迭代(x
)和几个常量参数(k
、d
)。这是一个非常简单的示例:
from multiprocessing import *
def test_function(args):
k = args[0]
d = args[1]
x = args[2]
del args
return k*x + d
if __name__ == '__main__':
pool = Pool(processes=2)
k = 3.
d = 5.
constants = [k,d]
xvalues = range(0,10)
result = [pool.apply_async(test_function, constants.append(i)) for i in xvalues]
output = [r.get() for r in result]
print output
#I expect [5.0, 8.0, 11.0, 14.0, 17.0, 20.0, 23.0, 26.0, 29.0, 32.0]
这给了我以下错误消息:
Traceback (most recent call last):
File "test_function.py", line 23, in <module>
output = [r.get() for r in result]
File "C:\Program Files\Python2.7\lib\multiprocessing\pool.py", line 528, in get
raise self._value
TypeError: test_function() argument after * must be a sequence, not NoneType
所以我的问题是:
此错误消息的实际含义是什么?
如何修复它以获得预期结果(请参阅最后一行代码示例)?
调用 apply_sync 的线路有更好/有效/优雅的方式吗?
仅供引用:我是 Python 新手,请耐心等待,如果我的帖子需要更多详细信息,请告诉我。
非常感谢您的建议!
最佳答案
What does this error message actually mean?
append
方法返回的值始终为 None
,因此执行时:
pool.apply_async(test_function, constants.append(i))
您正在使用 None
作为 args
参数调用 pool.apply_asynch
,但 apply_asynch
需要一个 可迭代作为参数。 apply_asynch
所做的事情称为 tuple
-unpacking .
How do I fix it to get the expected results?
要实现预期的输出,只需将 i
连接到常量即可:
pool.apply_asynch(test_function, (constants + [i],))
Is there a better/working/elegant way for the line that calls apply_sync?
请注意,您必须将所有参数包装到一个元素元组中,因为您的 test_function
接受单个参数。您可以这样修改它:
def test_function(k, d, x):
# etc
只需使用:
pool.apply_asynch(test_function, constants + [i])
apply_asynch
将使用 tuple
解包自动将 args
解包到函数的三个参数中。 (仔细阅读 Pool.apply
和 friend 的文档)。
Is there a better/working/elegant way for the line that calls apply_sync?
正如 Silas 所指出的,您应该使用 Pool.map
而不是对值列表使用 Pool.apply_asynch
。或 Pool.map_asynch
方法,它们可以为您完成此操作。
例如:
results = pool.map(test_function, [(constants + [i],) for i in xvalues])
但是请注意,在这种情况下 test_function
必须接受单个参数,因此您必须手动解压常量和 x
,就像您在问题中所做的那样。
此外,作为一般建议:
test_function
中,绝对不需要执行del args
。它只会减慢函数的执行速度(稍微减慢)。仅在需要时谨慎使用 del
。您可以使用以下语法,而不是手动分配元组中的元素:
k, d, x = args
这相当于(可能稍慢):
k = args[0]
d = args[1]
x = args[2]
多处理
来调用如此简单的函数,预计会减慢。通信和同步进程的成本相当大,因此您必须避免调用简单的函数,并尽可能尝试“分块”工作(例如,不要单独发送每个请求,而是将 100 个请求的列表发送给一个工作人员)论证)。关于python - 具有常量和可迭代参数的函数的多重处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18834153/
我试图为此搜索答案,但我发现很难找到这种“确切”的例子。我对指向指针的指针知之甚少,我觉得除了指向某物的指针之外,还有更多东西隐藏在它的表层之下。 那么你们会如何翻译呢? void free(sham
我有一个类的属性,比如const CFoo &bar() const,这是什么意思? 最佳答案 bar 方法返回对 const CFoo 的引用(即 bar 之前的 const CFoo & 部分),
例如是 int const x = 3; 有效代码? 如果是的话,意思是一样的 const int x = 3; ? 最佳答案 它们都是有效的代码并且它们都是等价的。对于指针类型,尽管它们都是有效代码
我知道 f(const T& obj) // (1) g(T const& obj) // (2) 是一样的。(我们不能改变f和g中obj的值)。 但是什么 h(T & const) // (3) 真
本节讲解的内容 include和include_once require和require_once 常量 引入文件和常量结合案列 变量操作函数 输出语句 前言 在上篇文章中,我们讨论了函数的应用,但是
我们知道我们可以保护变量的值,因此用户无法更改现有变量的值!这对对象来说没有什么问题吗?? 例如.. const x = 5; x = 10; alert(x) // will be returned
我正准备为 CUDA 设备编写直方图内核。它基于 NVIDIA's paper . 这个想法是每个线程计算某个部分(在我的例子中是体积)的部分直方图并将其写入共享内存块。然而,我遇到了一个奇怪的算法问
常量是固定值,程序执行期间不会改变。常量可以是任何基本数据类型,比如整数常量、浮点常量、字符常量或者字符串常量,还有枚举常量。 常量可以被当作常规的变量,只是它们的值在定义后不能被修改。 整数常
在这种情况下,如何识别是否有变量或字面量传递给函数 f()? 如何实现passed_as_constant()检查(见下面代码)? sub f { my $refStr=\$_[0]; ret
我目前想知道如何在 python 中列出 win32com 中的常量, 例如使用 excel win32com.client.Dispatch('Excel.Application') 有没有办法使用
这个问题在这里已经有了答案: PHP | define() vs. const (9 个回答) 关闭8年前。 在 PHP 中遇到常量问题想知道是否有人可以解释: 这行得通 const _ROOT =
我正在学习 Rust,到目前为止,似乎有 3 种声明变量的方法: const A: u8 = 42; static A: u8 = 42; let A: u8 = 42; 我知道你不能有一个可变的 c
我正在使用函数模板 void _createAttr(T)(args..., in T[]) 并使用 测试 T 的类型函数中的 static if(is(T == char)) 。当我打电话时, _c
这可能是一个天真的问题,我怀疑答案是"is",但我没有运气在这里和其他地方搜索“erlang编译器优化常量”等术语。无论如何,erlang 编译器是否可以(将)在编译时创建一个常量或文字的数据结构,并
我刚遇到这段 Java 脚本代码: const { myKey, uname, issorted, title, hClick, } = this.props; 请告诉我这是什么意
我正在努力实现以下目标: 我有一个父类,有一些逻辑。在子类中,我“重新定义”常量/属性。有没有办法让子属性可以通过父类中定义的方法访问?或者更具体地说 - 有什么方法可以强制“out”方法在下面的示例
如果这是个愚蠢的问题,请原谅。 我有一个带有内部类接口(interface)的“fragment ”外部类。该接口(interface)仅由另一个 Activity 类使用“implements Ou
我是 python 新手,尝试使用默认值并为类实例自定义它们。 因此,在这个示例中,我定义了一个 DEFAULT_STRING 和一个 DEFAULT_SETTINGS 变量,可以使用 customi
在 integer.xml 中,其形式为 0x001 0x002 是代码和 xml 文件都需要的存储常量。 C# 识别 Droid.Resource.Integer.foo,但它有一些大的
是否有跨平台(即跨 Linux、BSD 和 OS X,最好是所有 POSIX)我可以纯粹基于字符串 以编程方式访问诸如 O_RDWR 之类的常量>“O_RDWR”?我正在编写一些(非 C)代码,这些代
我是一名优秀的程序员,十分优秀!