Python多处理模块:在进程中调用实例方法

时间:2013-10-31 19:48:56

标签: python multiprocessing pickle

基于Python的多处理模块,我需要执行以下操作:

- 创建一个正在运行的进程,可以被特定事件中断。

- 在此过程中,从客户端接收消息,并将此消息传递给对象实例的处理程序方法。

下面的基本代码(省略了一些细节)。问题是我尝试调用实例方法(self.enroll(message),但没有效果,正如预期的那样。我知道原因 - 进程使用自己的内存等 - 我已经实现了Can't pickle <type 'instancemethod'> when using python's multiprocessing Pool.map()用于解决绑定有界方法的问题,以及尝试使用Manager,Queue,Pool的不同方法......因为没有工作,我决定尽可能地将代码设置为“原始”,以便您可以理解我的意图。欢迎任何帮助。

class DistManager:
    def __init__(self, name, network_address, password):
        self.name = name
        self.network_address = network_address
        self.password = password
        self.distribution_clients = {}

    def _run_distribution_process(self):
        import select
        while not self.should_stop_distribution_service.is_set():
            (sread, swrite, sexc) = select.select([self.distribution_listener], [], [], 0)
            if (sread):
                connection = self.distribution_listener.accept()
                serialized_message = connection.recv()  # currently only receiving
                connection.close()
                message = pickle.loads(serialized_message)
                self.enroll(message)  # THE PROBLEM IS HERE

    def start_distribution_service(self, distribution_port):
        self.distribution_port = distribution_port
        # patch for making Listener work with select.select during run
        Listener.fileno = lambda self: self._listener._socket.fileno()
        self.distribution_listener = Listener(address=(self.network_address, self.distribution_port),
                                              authkey=self.password)
        self.should_stop_distribution_service = Event()
        self.distribution_process = Process(name='Distribution Runner', target=self._run_distribution_process)
        self.distribution_process.daemon = True
        self.distribution_process.start()

    def stop_distribution_service(self):
        from time import sleep
        self.should_stop_distribution_service.set()
        sleep(1)
        self.distribution_listener.close()
        self.distribution_process.terminate()
        return self.distribution_process.exitcode

    def _enroll_distribution_client(self, identifier, network_address, phone_number):
        self.distribution_clients[identifier] = (network_address, phone_number)

    def enroll(self, message):
        if type(message.content) is tuple:
            self._enroll_distribution_client(message.generator_identifier, message.content[0], message.content[1])
        else:
            raise TypeError("Tuple expected")
        return message.code

1 个答案:

答案 0 :(得分:1)

你仍然可以使用multiprocessing.pool来解决这个错误。

将以下行添加到与该类进行多处理的代码中,您仍然可以通过池传递该方法。代码应该高于班级

import copy_reg
    import types

    def _reduce_method(meth):
        return (getattr,(meth.__self__,meth.__func__.__name__))
    copy_reg.pickle(types.MethodType,_reduce_method)

有关如何挑选方法的更多信息,请参阅下面的http://docs.python.org/2/library/copy_reg.html

相关问题