使用RPYC进行多处理Python" ValueError:禁用pickle"

时间:2014-11-13 00:06:18

标签: python multiprocessing python-multiprocessing rpyc

我正在尝试在rpyc服务中使用多处理包,但在尝试从客户端调用公开的函数时获取ValueError: pickling is disabled。我知道multiprocesing包使用酸洗来在进程之间传递信息,并且rpyc中不允许进行酸洗,因为它是一种不安全的协议。因此,我不确定使用rpyc进行多处理的最佳方式(或者无论如何)。如何在rpyc服务中使用多处理?这是服务器端代码:

import rpyc
from multiprocessing import Pool

class MyService(rpyc.Service):

    def exposed_RemotePool(self, function, arglist):

        pool = Pool(processes = 8)
        result = pool.map(function, arglist)
        pool.close()
        return result


if __name__ == "__main__":
    from rpyc.utils.server import ThreadedServer
    t = ThreadedServer(MyService, port = 18861)
    t.start()

以下是产生错误的客户端代码:

import rpyc

def square(x):
    return x*x

c = rpyc.connect("localhost", 18861)
result = c.root.exposed_RemotePool(square, [1,2,3,4])
print(result)

2 个答案:

答案 0 :(得分:2)

您可以在协议配置中启用pickle。配置存储为字典,您可以修改default并将其传递给服务器(protocol_config =)和客户端(config =)。您还需要在客户端和服务器端定义并行化的功能。以下是server.py的完整代码:

import rpyc
from multiprocessing import Pool
rpyc.core.protocol.DEFAULT_CONFIG['allow_pickle'] = True

def square(x):
    return x*x


class MyService(rpyc.Service):

    def exposed_RemotePool(self, function, arglist):

        pool = Pool(processes = 8)
        result = pool.map(function, arglist)
        pool.close()
        return result



if __name__ == "__main__":
    from rpyc.utils.server import ThreadedServer
    t = ThreadedServer(MyService, port = 18861, protocol_config = rpyc.core.protocol.DEFAULT_CONFIG)
    t.start()

对于client.py代码是:

import rpyc

rpyc.core.protocol.DEFAULT_CONFIG['allow_pickle'] = True

def square(x):
    return x*x

c = rpyc.connect("localhost", port = 18861, config = rpyc.core.protocol.DEFAULT_CONFIG)
result = c.root.exposed_RemotePool(square, [1,2,3,4])
print(result)

答案 1 :(得分:0)