获取"队列对象应该只在进程之间通过继承来共享"但我没有使用队列

时间:2017-11-07 17:19:28

标签: python python-multiprocessing concurrent.futures

我正在尝试使用ProcessPoolExecutor,但我收到了错误"队列对象应该只通过继承和#34;在进程之间共享,但我没有使用队列(至少没有明确)。我无法找到解释我做错的事情。

以下是一些演示问题的代码(不是我的实际代码):

from concurrent.futures import ProcessPoolExecutor, as_completed

class WhyDoesntThisWork:

    def __init__(self):
        self.executor = ProcessPoolExecutor(4)

    def execute_something(self, starting_letter):
        futures = [self.executor.submit(self.something, starting_letter, d) for d in range(4)]
        letter = None
        for future in as_completed(futures):
            letter = future.result()
        print(letter)

    def something(self, letter, d):
        # do something pointless for the example
        for x in range(d):
            letter = chr(ord(letter) + 1)

if __name__ == '__main__':
    WhyDoesntThisWork(). execute_something('A')

El Ruso指出,将something()作为静态方法或类方法会使错误消失。不幸的是,我的实际代码需要使用self来调用其他方法。

2 个答案:

答案 0 :(得分:3)

无需使用静态方法即可解决。

使用进程时,每个进程都在独立的内存空间中运行。这与使用线程不同,当不同的线程在相同的进程下使用相同的内存空间运行时,这是不同的。因此,使用时不会发生该错误 ThreadPoolExecutor,但发生在ProcessPoolExecutor

因此,当将类实例的函数传递到单独的子流程中时,多处理机制会对函数进行酸洗,以便可以将功能作为独立实例传递到子流程中。并且当子流程加入时,该类将通过未选出的函数实例进行更新。

要使其工作,只需将__getstate__()__setstate__()函数添加到类中,以指导该类如何腌制和释放该函数。在腌制中,可以排除不必要的字段,如del self_dict['executor']所示。

import multiprocessing
import time
from concurrent.futures import ProcessPoolExecutor, as_completed


class GuessItWorksNow():

    def __init__(self):
        self.executor = ProcessPoolExecutor(4)

    def __getstate__(self):
        state = self.__dict__.copy()
        del state['executor']
        return state

    def __setstate__(self, state):
        self.__dict__.update(state)

    def something(self, letter, d):
        # do something pointless for the example
        p = multiprocessing.current_process()
        time.sleep(1)
        for x in range(d):
            letter = chr(ord(letter) + 1)
        return (f'[{p.pid}] ({p.name}) ({letter})')

    def execute_something(self, starting_letter):
        futures = [self.executor.submit(self.something, starting_letter, d) for d in range(10)]
        for future in as_completed(futures):
            print(future.result())


if __name__ == '__main__':
    obj = GuessItWorksNow()
    obj.execute_something('A')

答案 1 :(得分:2)

尝试something

的此代码
@staticmethod
def something(letter, d):
    # do something pointless for the example
    for x in range(d):
        letter = chr(ord(letter) + 1)

或重构为:

from concurrent.futures import ProcessPoolExecutor, as_completed


class WhyDoesntThisWork:
    def something(self, letter, d):
        # do something pointless for the example
        for x in range(d):
            letter = chr(ord(letter) + 1)
        return letter


if __name__ == '__main__':
    executor = ProcessPoolExecutor(4)
    letter = 'A'
    obj = WhyDoesntThisWork()
    futures = [executor.submit(obj.something, letter, d) for d in range(4)]
    for future in as_completed(futures):
        print(future.result())