0rpc / zerorpc-python

zerorpc for python
http://www.zerorpc.io
Other
3.17k stars 378 forks source link

Subprocess client rpc call without response when main process have constructed a client #219

Open boscotsang opened 5 years ago

boscotsang commented 5 years ago

I have two rpc server and the code is below

# file Server.py

import zerorpc
class Server(object):
    def __init__(self):
        pass

    def add(self, a, b):
        return a + b

if __name__ == "__main__":
    s = zerorpc.Server(Server())
    s.bind("tcp://127.0.0.1:9888")
    s.run()
# file Server2.py

import zerorpc
class Server2(object):
    def __init__(self):
        pass

    def mul(self, a, b):
        return a * b

if __name__ == "__main__":
    s = zerorpc.Server(Server2())
    s.bind("tcp://127.0.0.1:9889")
    s.run()

I constructed a rpc client in main process and connect to Server and then start a subprocess to connect to Server2. However, the rpc call was stucked.

# file clinet2.py

import zerorpc
import multiprocessing as mp

def sub_func():
    sub_client = zerorpc.Client()
    sub_client.connect("tcp://127.0.0.1:9889")
    print(sub_client.mul(3, 3))

if __name__ == "__main__":
    client = zerorpc.Client()
    client.connect("tcp://127.0.0.1:9888")
    ps = mp.Process(target=sub_func, args=())
    ps.start()
    print(client.add(3, 3))
    ps.join()

When I start subprocess and then constructed client in main process, both calls sucess.

# file client2.py

import zerorpc
import multiprocessing as mp

def sub_func():
    sub_client = zerorpc.Client()
    sub_client.connect("tcp://127.0.0.1:9889")
    print(sub_client.mul(3, 3))

if __name__ == "__main__":
    ps = mp.Process(target=sub_func, args=())
    ps.start()
    client = zerorpc.Client()
    client.connect("tcp://127.0.0.1:9888")
    print(client.add(3, 3))
    ps.join()

Python version: 3.6.8 zerorpc version: 0.6.1