如何实现异步grpc python服务器? [英] How to implement a async grpc python server?

查看:419
本文介绍了如何实现异步grpc python服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为每个GRPC请求调用一个celery任务,并返回结果. 在默认的GRPC实现中,每个请求都在与线程池不同的线程中处理.

在我的情况下,服务器应该每秒以批处理方式处理约400个请求.因此,由于批处理,一个请求可能必须等待1秒钟才能得到结果,这意味着线程池的大小必须大于400,以避免阻塞.

这可以异步完成吗? 非常感谢.

class EventReporting(ss_pb2.BetaEventReportingServicer, ss_pb2.BetaDeviceMgtServicer):
  def ReportEvent(self, request, context):
    res = tasks.add.delay(1,2)
    result = res.get() ->here i have to block
    return ss_pb2.GeneralReply(message='Hello, %s!' % result.message)

解决方案

如果对res.get的调用可以异步完成(如果使用async关键字定义),则可以异步完成.

虽然grpc.server表示需要futures.ThreadPoolExecutor,但实际上它可以与任何会在传递它们的线程之外的某个线程上调用提交给它的行为.如果您将由您实现的grpc.server传递给grpc.server,而该futures.Executor仅使用一个线程对EventReporting.ReportEvent进行四百(或更多)个并发调用,则您的服务器应避免您描述的那种阻塞.

I need to call a celery task for each GRPC request, and return the result. In default GRPC implementation, each request is processed in a separate thread from a threadpool.

In my case, the server is supposed to process ~400 requests in batch mode per second. So one request may have to wait 1 second for the result due to the batch processing, which means the size of the threadpool must be larger than 400 to avoid blocking.

Can this be done asynchronously? Thanks a lot.

class EventReporting(ss_pb2.BetaEventReportingServicer, ss_pb2.BetaDeviceMgtServicer):
  def ReportEvent(self, request, context):
    res = tasks.add.delay(1,2)
    result = res.get() ->here i have to block
    return ss_pb2.GeneralReply(message='Hello, %s!' % result.message)

解决方案

It can be done asynchronously if your call to res.get can be done asynchronously (if it is defined with the async keyword).

While grpc.server says it requires a futures.ThreadPoolExecutor, it will actually work with any futures.Executor that calls the behaviors submitted to it on some thread other than the one on which they were passed. Were you to pass to grpc.server a futures.Executor implemented by you that only used one thread to carry out four hundred (or more) concurrent calls to EventReporting.ReportEvent, your server should avoid the kind of blocking that you describe.

这篇关于如何实现异步grpc python服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆