【问题标题】:Python AsyncHttpClient inside tornado RequestHandler throws exception龙卷风 RequestHandler 中的 Python AsyncHttpClient 抛出异常
【发布时间】:2019-01-30 12:10:40
【问题描述】:

我将在RequestHandler中通过tornado AsyncHttpClient调用一个端点,但是它抛出了运行时异常This event loop is already running

class RegistrationHandler(tornado.web.RequestHandler):

  def post(self, *args, **kwargs):
      call_async_register("some params")


def call_async_register(parameters):
    def call():
        http_client = AsyncHTTPClient()
        future = Future()
        http_request = HTTPRequest(url, request_type.name, headers={'X-Peering': '1'}, body=body)

        def handle_future(f: Future):
            future.set_result(f.result())

        fetched_future = http_client.fetch(http_request)

        fetched_future.add_done_callback(handle_future)

        return future
    try:
        instance = io_loop.IOLoop.current()
        response = instance.run_sync(call)
        return response.body.decode()
    except Exception as err:
        self.logger.exception("Account Request Failed: {}".format(err))
        return None

【问题讨论】:

    标签: python python-3.x asynchronous tornado


    【解决方案1】:

    问题来了:

    instance = io_loop.IOLoop.current()
    response = instance.run_sync(call)
    

    run_sync 本身尝试启动 ioloop。但从您的代码中可以明显看出,instance 已经在运行。所以你得到了错误。

    如果您想将call() 方法返回的值发送回用户,请将您的方法转换为协程(使用async/await 语法)。

    例子:

    class RegistrationHandler(tornado.web.RequestHandler):
    
        async def post(self, *args, **kwargs):
            response = await call_async_register("some params")
    
            self.write(response)
    
    
    async def call_async_register(parameters):
        http_client = AsyncHTTPClient()
        http_request = HTTPRequest(url, request_type.name, headers={'X-Peering': '1'}, body=body)
    
        try:
            response = await http_client.fetch(http_request)
            return response.body.decode()
        except Exception as err:
            self.logger.exception("Account Request Failed: {}".format(err))
            return None
    

    【讨论】:

    • @SuperHornet 要将call() 的结果返回给客户端吗?
    • 是的,我确实想回复
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多