Making synchronous requests using Tornado. RuntimeError: Cannot run the event loop while another loop is running
Making synchronous requests using Tornado. RuntimeError: Cannot run the event loop while another loop is running
我正在尝试使用 Tornado 发出同步请求。
已尝试拨打 API 次电话。
main.py
import tornado.web
import tornado.httpserver
import tornado.options
import tornado.ioloop
from tornado.web import url
from handlers import IndexHandler
from tornado.options import define, options
define("port", default=8000, help="run on the given port", type=int)
if __name__ == "__main__":
tornado.options.parse_command_line()
app = tornado.web.Application(
handlers=[
url(r"/", IndexHandler),
]
)
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
我的 handlers.py 是...
import tornado.web
import tornado.httpclient
import urllib
import json
import datetime
import time
class IndexHandler(tornado.web.RequestHandler):
def get(self):
query = self.get_argument('q')
client = tornado.httpclient.HTTPClient()
response = client.fetch("https://pokeapi.co/api/v2/pokemon/" + urllib.urlencode(("query")))
print(response)
if response == "Not Found":
self.write("Invalid Input")
response_data = json.loads(response.body)
pikachu_name = response_data['name']
pikachu_order = response_data['order']
self.write("""
Pickachu Name: %s <br>
Pickachu Order: %d <br>
""" %(pikachu_name, pikachu_order))
现在我面临的问题是
[E 200407 02:18:37 web:1792] Uncaught exception GET /?q=pikachu (::1)
HTTPServerRequest(protocol='http', host='localhost:8000', method='GET', uri='/?q=pikachu', version='HTTP/1.1', remote_ip='::1')
Traceback (most recent call last):
File "C:\Python\Python37\lib\site-packages\tornado\web.py", line 1701, in _execute
result = method(*self.path_args, **self.path_kwargs)
File "C:\Users\Bagga\Documents\tornado\Tweet Rate\handlers.py", line 12, in get
client = tornado.httpclient.HTTPClient()
File "C:\Python\Python37\lib\site-packages\tornado\httpclient.py", line 107, in __init__
self._async_client = self._io_loop.run_sync(make_client)
File "C:\Python\Python37\lib\site-packages\tornado\ioloop.py", line 526, in run_sync
self.start()
File "C:\Python\Python37\lib\site-packages\tornado\platform\asyncio.py", line 149, in start
self.asyncio_loop.run_forever()
File "C:\Python\Python37\lib\asyncio\base_events.py", line 529, in run_forever
'Cannot run the event loop while another loop is running')
RuntimeError: Cannot run the event loop while another loop is running
[E 200407 02:18:37 web:2250] 500 GET /?q=pikachu (::1) 55.85ms
问题是什么,我该如何解决?
我在这里看到的主要错误是,
RuntimeError:无法 运行 事件循环,而另一个循环正在 运行ning
我觉得这是由于 IOLoop 和 HTTPClient 造成的,但无法理解是什么问题?
我引用documentation of HTTPClient
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.
请改用AsyncHTTPClient
。
我正在尝试使用 Tornado 发出同步请求。
已尝试拨打 API 次电话。
main.py
import tornado.web
import tornado.httpserver
import tornado.options
import tornado.ioloop
from tornado.web import url
from handlers import IndexHandler
from tornado.options import define, options
define("port", default=8000, help="run on the given port", type=int)
if __name__ == "__main__":
tornado.options.parse_command_line()
app = tornado.web.Application(
handlers=[
url(r"/", IndexHandler),
]
)
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
我的 handlers.py 是...
import tornado.web
import tornado.httpclient
import urllib
import json
import datetime
import time
class IndexHandler(tornado.web.RequestHandler):
def get(self):
query = self.get_argument('q')
client = tornado.httpclient.HTTPClient()
response = client.fetch("https://pokeapi.co/api/v2/pokemon/" + urllib.urlencode(("query")))
print(response)
if response == "Not Found":
self.write("Invalid Input")
response_data = json.loads(response.body)
pikachu_name = response_data['name']
pikachu_order = response_data['order']
self.write("""
Pickachu Name: %s <br>
Pickachu Order: %d <br>
""" %(pikachu_name, pikachu_order))
现在我面临的问题是
[E 200407 02:18:37 web:1792] Uncaught exception GET /?q=pikachu (::1)
HTTPServerRequest(protocol='http', host='localhost:8000', method='GET', uri='/?q=pikachu', version='HTTP/1.1', remote_ip='::1')
Traceback (most recent call last):
File "C:\Python\Python37\lib\site-packages\tornado\web.py", line 1701, in _execute
result = method(*self.path_args, **self.path_kwargs)
File "C:\Users\Bagga\Documents\tornado\Tweet Rate\handlers.py", line 12, in get
client = tornado.httpclient.HTTPClient()
File "C:\Python\Python37\lib\site-packages\tornado\httpclient.py", line 107, in __init__
self._async_client = self._io_loop.run_sync(make_client)
File "C:\Python\Python37\lib\site-packages\tornado\ioloop.py", line 526, in run_sync
self.start()
File "C:\Python\Python37\lib\site-packages\tornado\platform\asyncio.py", line 149, in start
self.asyncio_loop.run_forever()
File "C:\Python\Python37\lib\asyncio\base_events.py", line 529, in run_forever
'Cannot run the event loop while another loop is running')
RuntimeError: Cannot run the event loop while another loop is running
[E 200407 02:18:37 web:2250] 500 GET /?q=pikachu (::1) 55.85ms
问题是什么,我该如何解决?
我在这里看到的主要错误是,
RuntimeError:无法 运行 事件循环,而另一个循环正在 运行ning
我觉得这是由于 IOLoop 和 HTTPClient 造成的,但无法理解是什么问题?
我引用documentation of HTTPClient
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.
请改用AsyncHTTPClient
。