pythonmultiprocessingpython-multiprocessingrpyc

Multiprocessing Python with RPYC "ValueError: pickling is disabled"


I am trying to use the multiprocessing package within an rpyc service, but get ValueError: pickling is disabled when I try to call the exposed function from the client. I understand that the multiprocesing package uses pickling to pass information between processes and that pickling is not allowed in rpyc because it is an insecure protocol. So I am unsure what the best way (or if there is anyway) to use multiprocessing with rpyc. How can I make use of multiprocessing within a rpyc service? Here is the server side code:

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()

And here is the client side code that produces the error:

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)

Solution

  • You can enable pickling in the protocol configuration. The configuration is stored as a dictionary, you can modify the default and pass it to both the server (protocol_config = ) and client (config =). You also need to define the function being parallelized on both the client and server side. So here is the full code for 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()
    

    And for client.py the code is:

    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)