GRPC调用不适用于多Python进程

时间:2019-04-18 14:41:36

标签: python-2.7 multiprocessing python-multiprocessing grpc

在多个进程中运行时,GRPC调用不起作用。但是在主进程/一个新生成的进程上运行时,可以执行任意多次。创建第二个新进程后,所有GRPC调用都会失败。

我使用future.ThreadPoolExecutor设置了GRPC服务器: start_server()方法

    some_server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    some_server_pb2_grpc.add_SomeInterfaceServicer_to_server(Server(), some_server)

    # bind the server to the port defined above
    some_server.add_insecure_port('[::]:{}'.format(self.server_port))

    # start the server
    some_server.start()

我的python代码:

# GRPC TEST - 1 : WORKS!! Single process calling GRPC client

from grpc_module.client import Client
from multiprocessing.process import Process
from utils import send_status_info


send_status_info() # Works

def my_loop():
    for i in range(10):
        send_status_info()

p = Process(target=my_loop)
p.start() # Works
p.join()

但是当完成多个进程时,GRPC调用开始失败。

# GRPC TEST - 2 : FAILS!! Multiple process calling GRPC client

from grpc_module.client import Client
from multiprocessing.process import Process
from utils import send_status_info


send_status_info()

def my_loop():
    for i in range(10):
        send_status_info()

p = Process(target=my_loop)
p.start()
p.join()

p = Process(target=my_loop)
p.start()
p.join()

这与一元GRPC设置有关。

如果是,我如何在此处实现异步GRPC?

请帮助。

更新:我的发现-GRPC调用到达了GRPC客户端代码,但是在与服务器联系时失败。使用python命令行运行此命令时,当它卡住时(如方法2中所示),我按ctrl + C,它将以如下所示的堆栈跟踪终止:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/multiprocessing/process.py", line 145, in join
    res = self._popen.wait(timeout)
  File "/usr/lib/python2.7/multiprocessing/forking.py", line 154, in wait
    return self.poll(0)
  File "/usr/lib/python2.7/multiprocessing/forking.py", line 135, in poll
    pid, sts = os.waitpid(self.pid, flag)
KeyboardInterrupt

0 个答案:

没有答案