Search code examples
asynchronoustornado

Tornado async endpoint does not works


I have two microservices:

  • a service

Tornado service with two endpoints: /foo and /bar

/foo

async def get(...):
   x = await test()
   return x

async def test():
    y = call to b service, FooBar rpc
    return y

/bar

 def get(...):
   return True
  • b service

gRPC service with rpc FooBar

rpc FooBar

def FooBar(...):
   return requests.get("/bar")

If client hits endpoint /foo in a service:

  1. Code hits rpc FooBar in b service
  2. FooBar rpc can't hits /bar endpoint in a service as that service is blocked.

AFAIK, using x=await test() should prevent us of such blocking, what I have missed?


Solution

  • Since the rpc calls aren't async, it will block the Tornado process.

    You can avoid blocking the main process by running the rpc calls in a separate thread.

    First, make the test() method regular function, not a coroutine (remove the async keyword).

    Example code:

    async def get(...): 
        x = await IOLoop.current().run_in_executor(None, test)
        return x
    
    # regular function, not async
    def test(...):
        # make calls
        return x