async 和 asyncio 错误 -- TypeError: 'coroutine' object is not callable
Posted
技术标签:
【中文标题】async 和 asyncio 错误 -- TypeError: \'coroutine\' object is not callable【英文标题】:async and asyncio error -- TypeError: 'coroutine' object is not callableasync 和 asyncio 错误 -- TypeError: 'coroutine' object is not callable 【发布时间】:2018-06-29 18:57:13 【问题描述】:我试图让一个函数在 Python3.x 的 Flask 应用程序中异步运行。我正在使用asyncio
库并将async
放在我的函数声明之外,但我收到一条错误消息:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1615, in full_dispatch_request
return self.finalize_request(rv)
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1630, in finalize_request
response = self.make_response(rv)
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1740, in make_response
rv = self.response_class.force_type(rv, request.environ)
File "/usr/local/lib/python3.6/site-packages/werkzeug/wrappers.py", line 885, in force_type
response = BaseResponse(*_run_wsgi_app(response, environ))
File "/usr/local/lib/python3.6/site-packages/werkzeug/test.py", line 884, in run_wsgi_app
app_rv = app(environ, start_response)
TypeError: 'coroutine' object is not callable
我的代码如下:
import asyncio
... more function definitions ...
@app.route("/save_to_dbx")
@login_required
async def save_to_dbx():
""" Saves a course to Dropbox as a .zip """
# Creating a .zip file out of the course
zip = zipfile.ZipFile("IoT.zip", "w")
for subdir, dirs, files in os.walk(os.path.join(BASE_PATH, "static/Content/IoT")):
for file in files:
complete__file_path = os.path.join(subdir, file)
print("Writing to Zip:", complete__file_path)
zip.write(complete__file_path, complete__file_path.split("/")[-1])
zip.close()
# Uploading the .zip to Dropbox
f = open(os.path.join(BASE_PATH, "IoT.zip"))
file_size = os.path.getsize(os.path.join(BASE_PATH, "IoT.zip"))
CHUNK_SIZE = 4 * 1024 * 1024
print("Upload file size:", file_size)
if file_size <= CHUNK_SIZE:
print(__dbx_conn__().files_upload(f, "/VOSW-Backup-Testing/IoT.zip"))
else:
upload_session_start_result = __dbx_conn__().files_upload_session_start(f.read(CHUNK_SIZE))
cursor = dropbox.files.UploadSessionCursor(
session_id=upload_session_start_result.session_id,
offset=f.tell())
commit = dropbox.files.CommitInfo(path="/VOSW-Backup-Testing/IoT.zip")
while f.tell() < file_size:
if ((file_size - f.tell()) <= CHUNK_SIZE):
print(__dbx_conn__().files_upload_session_finish(f.read(CHUNK_SIZE), cursor, commit))
else:
__dbx_conn__().files_upload_session_append_v2(f.read(CHUNK_SIZE), cursor)
cursor.offset = f.tell()
return """<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Success!</title>
</head>
<body>
<h1>Success!</h1>
</body>"""
是什么导致了这个错误?
【问题讨论】:
【参考方案1】:flask
不是异步的。它根本不与asyncio
直接兼容,并且在flask 应用程序中将async
函数传递给@app.route
将不起作用。
我建议改用quart。
【讨论】:
哦,夸脱看起来很整洁。只使用通用线程怎么样?基本上我想上传到 Dropbox 但它超级慢,所以我希望进程在后台运行。 @agaidis flask 默认已经使用线程——每个请求都在自己的线程中运行,所以你不需要做任何事情,当你上传时,你的应用程序仍然能够回答其他请求。不过我更喜欢异步 感谢您的回复。它可能能够接受额外的请求,但是当用户等待将 1GB .zip 上传到 Dropbox 时,网页会“冻结”。我所在的小组与 Flask 非常相关,所以如果 quart 真的会飞的话,我想知道。 Flask 中是否有任何你能想到的实现这种后台处理的方法? @agaidis 我仍然建议你应该使用异步网络——这是一个完美的用例;解决它的其他常见方法是将消息队列卸载到工作进程。但是您的进程只想等待网络,所以异步是要走的路【参考方案2】:您应该考虑使用Sanic。它非常快并且可以很好地处理异步请求。
有了sanic,你可以考虑this解决方案。
【讨论】:
以上是关于async 和 asyncio 错误 -- TypeError: 'coroutine' object is not callable的主要内容,如果未能解决你的问题,请参考以下文章
Django — async_to_sync 与 asyncio.run