Grzegorzg
Grzegorzg

Reputation: 719

Tornado async endpoint does not works

I have two microservices:

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

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?

Upvotes: 0

Views: 103

Answers (1)

xyres
xyres

Reputation: 21844

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

Upvotes: 1

Related Questions