为什么我不能使用python模块同期期货课堂教学法?

2024-09-20 23:01:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我想让我的类方法并行运行,但它只会产生一些我无法解决的错误。 我的代码是:

import concurrent.futures as futures

samples = ['asfd', 'zxcv', 'asf', 'qwer']

class test:
    def __init__(self, samples):
        maturedb = {}
        with futures.ProcessPoolExecutor() as exe:
            for samplename, dResult in exe.map(self.make_readdb, samples):
                maturedb[samplename] = dResult
        print(maturedb)

    def make_readdb(self, samplename):
        return samplename, 1

test(samples)

如果我在Ubuntu machine中运行此代码,会出现如下错误:

^{pr2}$

make_readdb方法只是简化为一个示例,但它是实际代码和 我需要让它平行。请帮忙。在


Tags: 方法代码testselfmakedefas错误
1条回答
网友
1楼 · 发布于 2024-09-20 23:01:02

docs

The ProcessPoolExecutor class is an Executor subclass that uses a pool of processes to execute calls asynchronously. ProcessPoolExecutor uses the multiprocessing module, which allows it to side-step the Global Interpreter Lock but also means that only picklable objects can be executed and returned.

试试ThreadPoolExecutor

更新

我又看了一遍代码,问题是函数make_readdb-是test类的成员。你能重构这个功能吗?在

相关问题 更多 >

    热门问题