日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

RuntimeError: Cannot run the event loop while another loop is running

發布時間:2023/12/31 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 RuntimeError: Cannot run the event loop while another loop is running 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

代碼如下:

import tornado.ioloop import tornado.web from tornado.httpclient import HTTPClient, AsyncHTTPClientclass MainHandler(tornado.web.RequestHandler):# 同步def get(self):h_c = HTTPClient()res = h_c.fetch("http://www.baidu.com")# print(res)# passself.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:passself.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

根據官方文檔:

Changed in version 5.0:?Due to limitations in?asyncio, it is no longer possible to use the synchronous?HTTPClient?while an?IOLoop?is running. Use?AsyncHTTPClient?instead.

解決方案:

Tornado同步api和異步api混寫一例

總結

以上是生活随笔為你收集整理的RuntimeError: Cannot run the event loop while another loop is running的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。