龙卷风RequestHandler中的Python AsyncHttpClient引发异常

时间:2019-01-30 12:10:40

标签: python python-3.x asynchronous tornado

我将在RequestHandler中通过龙卷风AsyncHttpClient调用端点,但是它将引发运行时异常local function goMenu() composer.removeScene("menu") composer.gotoScene("menu","slideUp",500) return true end

This event loop is already running

1 个答案:

答案 0 :(得分:2)

问题出在这里

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