英文:
Python Flask TypeError: 'async_generator' object is not iterable
问题
我正在尝试在Flask中创建一个Telegram文件流程。
我使用的代码如下:
from flask import Response
@app.route("/dl/<hash>", methods=["GET"])
async def download(hash):
body = yield_file()
return Response(
status=206 if range_header else 200,
response=body,
headers={
"Content-Type": f"{mime_type}",
"Content-Range": f"bytes {from_bytes}-{until_bytes}/{file_size}",
"Content-Length": str(req_length),
"Content-Disposition": f'{disposition}; filename="{file_name}"',
"Accept-Ranges": "bytes",
},
)
其中yield_file()
函数我从 https://github.com/EverythingSuckz/TG-FileStreamBot/blob/3f7304fa023e373e873c05abe431aafd537020fe/WebStreamer/utils/custom_dl.py#L164 中获取的。
在该存储库中,他使用了aiohttp web服务器,那里工作正常。
但在Flask中,它出现了以下错误:
127.0.0.1 - - [03/Aug/2023 22:21:06] "GET /dl/jRtYoKqEu6 HTTP/1.1" 500 -
Error on request:
Traceback (most recent call last):
File "C:\Users\hp\AppData\Roaming\Python\Python310\site-packages\werkzeug\serving.py", line 364, in run_wsgi
execute(self.server.app)
File "C:\Users\hp\AppData\Roaming\Python\Python310\site-packages\werkzeug\serving.py", line 327, in execute
for data in application_iter:
File "C:\Users\hp\AppData\Roaming\Python\Python310\site-packages\werkzeug\wsgi.py", line 289, in __next__
return this._next()
File "C:\Users\hp\AppData\Roaming\Python\Python310\site-packages\werkzeug\wrappers\response.py", line 32, in _iter_encoded
for item in iterable:
TypeError: 'async_generator' 对象不可迭代
我能知道问题在哪里以及如何修复吗?
编辑:
它使用了 for
关键字而不是 async for
。
英文:
i am trying to make a file streamer from telegram in flask
code i used
from flask import Response
@app.route("/dl/<hash>", methods=["GET"])
async def download(hash):
body = yield_file()
return Response(
status=206 if range_header else 200,
response=body,
headers={
"Content-Type": f"{mime_type}",
"Content-Range": f"bytes {from_bytes}-{until_bytes}/{file_size}",
"Content-Length": str(req_length),
"Content-Disposition": f'{disposition}; filename="{file_name}"',
"Accept-Ranges": "bytes",
},
)
where the yield_file() function i took from https://github.com/EverythingSuckz/TG-FileStreamBot/blob/3f7304fa023e373e873c05abe431aafd537020fe/WebStreamer/utils/custom_dl.py#L164
in the repo he used aiohttp web server and it works fine there
but in flask it gives the following error
127.0.0.1 - - [03/Aug/2023 22:21:06] "GET /dl/jRtYoKqEu6 HTTP/1.1" 500 -
Error on request:
Traceback (most recent call last):
File "C:\Users\hp\AppData\Roaming\Python\Python310\site-packages\werkzeug\serving.py", line 364, in run_wsgi
execute(self.server.app)
File "C:\Users\hp\AppData\Roaming\Python\Python310\site-packages\werkzeug\serving.py", line 327, in execute
for data in application_iter:
File "C:\Users\hp\AppData\Roaming\Python\Python310\site-packages\werkzeug\wsgi.py", line 289, in __next__
return self._next()
File "C:\Users\hp\AppData\Roaming\Python\Python310\site-packages\werkzeug\wrappers\response.py", line 32, in _iter_encoded
for item in iterable:
TypeError: 'async_generator' object is not iterable
can i know whats the problem here and how to fix that
Edit :
Maybe the problem is because of this https://github.com/pallets/werkzeug/blob/41b74e7a92685bc18b6a472bd10524bba20cb4a2/src/werkzeug/wrappers/response.py#L32
It is using for
keyword instead of async for
答案1
得分: 1
这应该可以工作:
import asyncstdlib
async for i, numbers in asyncstdlib.enumerate(gen_random_numbers()):
print(f"使用批次 {i} 并处理 {numbers}")
英文:
This should work:
import asyncstdlib
async for i, numbers in asyncstdlib.enumerate(gen_random_numbers()):
print(f"working with the batch {i} and processing {numbers}")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论