相关疑难解决方法(0)

如何将具有多个参数的函数传递给python concurrent.futures.ProcessPoolExecutor.map()?

我想concurrent.futures.ProcessPoolExecutor.map()调用一个由2个或更多参数组成的函数.在下面的示例中,我使用了一个lambda函数并将其定义refnumberlist具有相同值的相同大小的数组.

第一个问题:有更好的方法吗?在numberlist的大小可能是数百万到数十亿个元素的情况下,因此ref大小必须遵循numberlist,这种方法不必要地占用宝贵的内存,我想避免.我这样做是因为我读取map函数将终止其映射,直到达到最短的数组结束.

import concurrent.futures as cf

nmax = 10
numberlist = range(nmax)
ref = [5, 5, 5, 5, 5, 5, 5, 5, 5, 5]
workers = 3


def _findmatch(listnumber, ref):    
    print('def _findmatch(listnumber, ref):')
    x=''
    listnumber=str(listnumber)
    ref = str(ref)
    print('listnumber = {0} and ref = {1}'.format(listnumber, ref))
    if ref in listnumber:
        x = listnumber
    print('x = {0}'.format(x))
    return x 

a = map(lambda x, y: _findmatch(x, y), numberlist, ref)
for n …
Run Code Online (Sandbox Code Playgroud)

python concurrency lambda python-3.x concurrent.futures

12
推荐指数
3
解决办法
9159
查看次数

是否可以在事件之后使用 concurrent.futures 在 tkinter 类中执行函数/方法?如果是,如何?

我正在尝试使用提供的工作人员池concurrent.futures.ProcessPoolExecutor来加快 tkinter 类中方法的性能。这是因为执行该方法是 CPU 密集型的,并且“并行化”它应该缩短完成它的时间。我希望根据控件对它的性能进行基准测试 - 相同方法的串行执行。我已经编写了一个 tkinter GUI 测试代码来执行这个基准测试。该方法的串行执行有效,但并发部分无效。感谢让我的代码的并发部分工作的任何帮助。

更新:我已确保我已正确实施concurrent.futures.ProcessPoolExecutor以解决 Tk() 之外的问题,即来自标准的 python3 脚本。在这个答案中有解释。现在我想实现该答案中描述的并发方法来使用我的 tkinter.Tk() GUI 中的按钮。

我的测试代码如下。当你运行它时,会出现一个 GUI。当您点击“FIND”按钮时,_findmatch 函数会以串行和并发的方式执行,以查找数字 5 在 0 到 1E8 的数字范围内出现的次数。串行部分工作,但并发部分抱怨(见下文)。任何人都知道如何解决这个酸洗错误?

Traceback (most recent call last):
  File "/usr/lib/python3.5/multiprocessing/queues.py", line 241, in _feed
    obj = ForkingPickler.dumps(obj)
  File "/usr/lib/python3.5/multiprocessing/reduction.py", line 50, in dumps
    cls(buf, protocol).dump(obj)
_pickle.PicklingError: Can't pickle <class '_tkinter.tkapp'>: attribute lookup tkapp on _tkinter failed
Run Code Online (Sandbox Code Playgroud)

测试代码:

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import tkinter as tk # …
Run Code Online (Sandbox Code Playgroud)

python concurrency tkinter concurrent.futures python-3.5

2
推荐指数
1
解决办法
3961
查看次数