- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试编写一个基于 multiprocessing
的便捷函数库,它接受任何函数和参数,并使用多个进程运行该函数。我正在导入以下文件“MultiProcFunctions.py”:
import multiprocessing
from multiprocessing import Manager
def MultiProcDecorator(f,*args):
"""
Takes a function f, and formats it so that results are saved to a shared dict
"""
def g(procnum,return_dict,*args):
result = f(*args)
return_dict[procnum] = result
return g
def MultiProcFunction(f,n_procs,*args):
"""
Takes a function f, and runs it in n_procs with given args
"""
manager = Manager()
return_dict = manager.dict()
jobs = []
for i in range(n_procs):
p = multiprocessing.Process( target = f, args = (i,return_dict) + args )
jobs.append(p)
p.start()
for proc in jobs:
proc.join()
return dict(return_dict)
这是我运行的代码:
from MultiProcFunctions import *
def sq(x):
return [i**2 for i in x]
g = MultiProcDecorator(sq)
if __name__ == '__main__':
result = MultiProcFunction(g,2,[1,2,3])
我收到以下错误:PicklingError: Can't pickle <function g at 0x01BD83B0>: it's not found as MultiProcFunctions.g
如果我对 g
使用以下定义相反,一切都很好:
def g(procnum,return_dict,x):
result = [i**2 for i in x]
return_dict[procnum] = result
为什么g
的两个定义是不同,我能做些什么来得到原来的 g
“工作”的定义?
最佳答案
尝试 dano's trick似乎只适用于 Python 2。在 Python 3 中尝试时,出现以下错误:
pickle.PicklingError: Can't pickle <function serialize at 0x7f7a1ac1fd08>: it's not the same object as __main__.orig_fn
我通过从 worker 的 init 中“装饰”函数解决了这个问题:
from functools import wraps
import sys
def worker_init(fn, *args):
@wraps(fn)
def wrapper(x):
# wrapper logic
pass
setattr(sys.modules[fn.__module__], fn.__name__, wrapper)
pool = mp.Pool(initializer=worker_init, initargs=[orig_fn, *args])
# ...
关于python - 将 multiprocessing 与修饰函数一起使用会导致 PicklingError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26576550/
我在使用 lambda 表达式时遇到问题,该表达式用于自定义过滤来自 DataGridView 的数据。 这是表达式: private static Expression> ExpressionLon
我想使用装饰器模式扩展 basic_streambuf 对象的行为。这就是我目前得到的: template class forwarding_basic_streambuf : boost::
我正在编写一个 Backbone 应用程序,我想编写一个经过身份验证的装饰器,我可以用它来装饰路由器类中的方法(路由)列表。 所以我有一个带有几种方法的路由器,并尝试过类似的方法。但是,当我调用我想要
我想重写 alert() Javascript 函数,同时保留它的“聚焦”行为。即,自定义方面,同时将焦点带到打开弹出窗口的浏览器选项卡。 最佳答案 无法直接自定义/装饰 JavaScript 警报框
我们如何在 jquery mobile 中实现事件行为内部的 touch up。 我希望它的行为与 native ios 按钮完全相同。我们可以点击并按住点击并在按钮内移动手指只有当我在按钮内释放时才
我正在尝试覆盖模块(connect-mongo)中的类。该模块的工作原理大致如下: 在server.js中: const session = require('express-session'); c
我希望使用托管代码在非托管 C++ DLL 上加载库,然后在已损坏的外部函数上调用 GetProcAddress。我的问题是,您从 C++ 编译器获得的错位名称是否具有确定性?即:如果原始签名未更改,
为什么我不能使用 @staticmethod 装饰器使类的 __call__ 方法静态化? class Foo(object): @staticmethod def bar():
我有一个小的 Delphi 10.3.3 应用程序,它有一些文本编辑功能,使用用户输入文本的 TMemo。 我正在尝试包含一些格式选项,如本网站提供的内容: http://qaz.wtf/u/conv
这是我关于使用 Swift 运行的 iOS 应用程序的情况。 我有一个下载按钮,触摸后即可开始下载文件: @IBAction func downloadButtonPressed() { if
我有一个 View ,我需要检测一个属性是否用隐藏输入装饰。 我的属性(property)被定义为: [HiddenInput(DisplayValue = false)] public string
我在我的 Qt 应用程序中绘制线和点,但在 QPainter 中似乎存在缩放错误时遇到了问题。我的线条工作得很好,但点会受到某些缩放转换的影响,尽管笔被设置为“化妆品”。最让我困扰的是似乎只有一个轴(
我在 Eclipse 中遇到 Java 语法着色问题。我正在尝试更改 Eclipse 中 JUST java 修饰符的文本颜色(如公共(public)、 protected 、私有(private))
我在站点上有一些限制区域,我想为其指定 login_required 装饰器。但是我想在主 urls.py 中每次包含一次,而不是包含 urls.py 中的每个单独的 url 所以代替: /priva
这个建议似乎很有意义,因为该方法中没有使用 self,但现在我很好奇: 这是对 CBV 的疏忽吗?用户应该手动装饰每个 @staticmethod? Django 中是否已经有一些代码可以自动使所有
python 3.3:我可以装饰一个函数来忽略所有与命名参数不匹配的位置参数吗? import functools def ignore_star_args(): def wrapper(fu
我是一名优秀的程序员,十分优秀!