5

我试图在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)
4

2 回答 2

4

您可以在协议配置中启用酸洗。配置存储为字典,您可以修改默认值并将其传递给服务器 ( 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)
于 2014-11-26T00:29:05.530 回答
0

您应该在协议配置中启用它。请参阅http://rpyc.readthedocs.org/en/latest/api/core_protocol.html#rpyc.core.protocol.DEFAULT_CONFIG

于 2014-11-13T09:28:28.947 回答