RuntimeError: Cannot run the event loop while another loop is running(目前没有解决)

代码如下:

import tornado.ioloop
import tornado.web
from tornado.httpclient import HTTPClient, AsyncHTTPClient

class MainHandler(tornado.web.RequestHandler):
    # 同步
    def get(self):
        h_c = HTTPClient()
        res = h_c.fetch("http://www.baidu.com")
        # print(res)
        # pass
        self.write("Hello, world")


class TestHandler(tornado.web.RequestHandler):
    # 异步
    async def get(self):
        http_client = AsyncHTTPClient()
        try:
            res = await http_client.fetch("http://www.baidu.com")
        except Exception as e:
            print("Error: %s" % e)
        else:
            pass
        self.write("Hello, world1")


def make_app():
    return tornado.web.Application([
        (r"/test", TestHandler),
        (r"/", MainHandler),
    ])


if __name__ == "__main__":
    app = make_app()
    app.listen(8888)
    tornado.ioloop.IOLoop.current().start()

浏览器打开:

http://127.0.0.1:8888/

完整报错:

ERROR:tornado.application:Uncaught exception GET / (127.0.0.1)
HTTPServerRequest(protocol='http', host='127.0.0.1:8888', method='GET', uri='/', version='HTTP/1.1', remote_ip='127.0.0.1')
Traceback (most recent call last):
  File "/home/appleyuchi/anaconda3/envs/Python3.6/lib/python3.6/site-packages/tornado/web.py", line 1590, in _execute
    result = method(*self.path_args, **self.path_kwargs)
  File "test.py", line 8, in get
    h_c = HTTPClient()
  File "/home/appleyuchi/anaconda3/envs/Python3.6/lib/python3.6/site-packages/tornado/httpclient.py", line 95, in __init__
    gen.coroutine(lambda: async_client_class(**kwargs)))
  File "/home/appleyuchi/anaconda3/envs/Python3.6/lib/python3.6/site-packages/tornado/ioloop.py", line 571, in run_sync
    self.start()
  File "/home/appleyuchi/anaconda3/envs/Python3.6/lib/python3.6/site-packages/tornado/platform/asyncio.py", line 132, in start
    self.asyncio_loop.run_forever()
  File "/home/appleyuchi/anaconda3/envs/Python3.6/lib/python3.6/asyncio/base_events.py", line 432, in run_forever
    'Cannot run the event loop while another loop is running')
RuntimeError: Cannot run the event loop while another loop is running
ERROR:tornado.access:500 GET / (127.0.0.1) 3.29ms

发布了824 篇原创文章 · 获赞 394 · 访问量 175万+

猜你喜欢

转载自blog.csdn.net/appleyuchi/article/details/105416142