使用 Python/Tornado 时,是否可以通过处理程序中的 http 请求调用另一个 API?

When using Python/Tornado, is it possible to call another API thru http request within a handler?

我编写了一组 python REST API,由 Tornado Web 框架提供服务。我面临的问题是这样的:在处理 endpoint1 或 API1 时,我需要获取 endpoint2 或 API2 可以提供的一些数据。因此,在端点 1 的处理程序中,我调用如下内容:

   class endpoint1(tornado.web.RequestHandler):
    .........   

      def get(self):
      ..........
         http_client = AsyncHTTPClient()
         url = "http://127.0.0.1:8686/v1/endpoint2"
         response = yield http_client.fetch(url)

但是,代码此时挂起。我的猜测是它不起作用,因为该框架目前正在为 endpoint1 提供服务,而我正试图在其中加入另一个请求。我正在寻找有关如何在不使用 MQ 或数据库的情况下使其工作的建议。 我也尝试使用 nest_asyncio - 没有骰子。任何帮助表示赞赏

事实证明 nest_asyncio 确实有效。这是另一个解释得很好的线程的 link:RuntimeError: This event loop is already running in python

import nest_asyncio
nest_asyncio.apply()

 class endpoint1(tornado.web.RequestHandler):
.........   

  def get(self):
  ..........
     http_client = AsyncHTTPClient()
     url = "http://127.0.0.1:8686/v1/endpoint2"
     response = await http_client.fetch(url)