gRPC 服务器内的 Python ProcessPoolExecutor 进程在没有抛出任何错误的情况下死亡

Jam*_*mes 6 python multithreading concurrent.futures grpc-python

我有一个gRPC它使用的服务器ProcessPoolExecutormax_workers=1运行一个脚本。就是为什么我不得不使用ProcessPoolExecutor里面run_brain_applicationmax_workers=1。下面是服务器脚本的简化版本。

我已经放置了两个打印语句来打印process.

问题是有时,对于某些请求,该过程开始但从未完成。我的意思是它打印START,但我从来没有看到STOP. 下一次相同的请求有效,因此问题不在于请求。此外,如果 内有异常run_application,它会毫无问题地抛出。

我已经检查了 Stackoverflow 上的所有相关问题,但其中大多数(12)都是关于他们的进程没有抛出异常,这不是我的情况。

任何想法都非常感谢!

谢谢

class BrainServer(brain_pb2_grpc.BrainServiceServicer):

    def run_brain_application(self, request, context):
            request_dict = MessageToDict(
                request, 
                preserving_proto_field_name=True,
                keep_null=True
            )

            print("START")
            with futures.ProcessPoolExecutor(max_workers=1) as executor:
                result = executor.submit(run_application, request_dict).result()
            res = result.get('data')
            print("STOP")
            report = dict_to_protobuf(result_pb2.Result, {res_type: res}, strict=False)

            return report

        except Exception as e:
            _log_fatal(
                msg=str(e)
            )
            raise e


def serve():
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=16))
    brain_pb2_grpc.add_BrainServiceServicer_to_server(BrainServer(), server)

    server.add_insecure_port(f"[::]:{GRPC_PORT}")
    server.start()
    server.wait_for_termination()

if __name__ == '__main__':
    serve()
Run Code Online (Sandbox Code Playgroud)

ofi*_*ule 0

这是一个很难确定的问题。

我首先建议为您的执行程序添加超时。

另外,我建议仅使用ThreadPoolExecutor并查看问题是否得到解决。在多进程环境中使用时, Pythonlogger和其他一些组件的表现不佳。。我的猜测是你正在使用 python 资源,这些资源不应该跨进程共享,但它们是(这logger是我的直接怀疑)