![](/img/trans.png)
[英]How to fix RuntimeWarning: Enable tracemalloc to get the object allocation traceback error
[英]How to fix “RuntimeWarning: Enable tracemalloc to get the object allocation traceback” when using tornado.httpclient.AsyncHTTPClient?
我用tornado.httpclient.AsyncHTTPClient
在我的龍卷風Web應用程序” headler。
這是我的代碼
class CustomTornadoHandler(tornado.web.RequestHandler):
def set_default_headers(self):
self.set_header("Access-Control-Allow-Origin", "*")
self.set_header("Access-Control-Allow-Headers", "x-requested-with,application/x-www-form-urlencoded")
self.set_header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PATCH, DELETE, PUT')
def initialize(self, *args, **kwargs):
self.db_session = db_session()
def on_finish(self):
db_session.remove()
class AdminUploadAlignerParagraphTaskHandler(CustomTornadoHandler):
executor = concurrent.futures.ThreadPoolExecutor()
@run_on_executor
def post(self):
async def f():
http_client = tornado.httpclient.AsyncHTTPClient()
try:
response = await http_client.fetch("http://www.google.com")
except Exception as e:
print("Error: %s" % e)
else:
logging.info(response.body)
...
self.write("")
f()
我在https://www.tornadoweb.org/en/stable/httpclient.html中獲得了示例。 但它不起作用:
RuntimeWarning: coroutine 'AdminUploadAlignerParagraphTaskHandler.post.<locals>.f' was never awaited
f()
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
我該怎么辦?
函數f()
是一個協程,你只是在沒有等待的情況下調用它。 你需要使用await f()
來調用它。 要實現這一點,您還需要將post
方法轉換為協程。
你不必要地使post
方法復雜化。 我不明白你為什么要在一個單獨的線程上運行它。
這是我如何重寫它:
# no need to run on separate thread
async def post():
http_client = tornado.httpclient.AsyncHTTPClient()
try:
response = await http_client.fetch("http://www.google.com")
except Exception as e:
print("Error: %s" % e)
else:
logging.info(response.body)
...
self.write("")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.