Python Flask TypeError:“async_generator”对象不可迭代

问题描述 投票:0回答:1

我正在尝试用烧瓶中的电报制作一个文件流媒体

我使用的代码

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",
        },
    )

我从https://github.com/EverythingSuckz/TG-FileStreamBot/blob/3f7304fa023e373e873c05abe431aafd537020fe/WebStreamer/utils/custom_dl.py#L164

获取yield_file()函数

在仓库中他使用了 aiohttp Web 服务器并且它在那里工作正常

但在烧瓶中它给出以下错误

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

我可以知道这里有什么问题以及如何解决吗

编辑:

也许问题是因为这个https://github.com/pallets/werkzeug/blob/41b74e7a92685bc18b6a472bd10524bba20cb4a2/src/werkzeug/wrappers/response.py#L32

它使用

for
关键字而不是
async for

python flask streaming telegram
1个回答
1
投票

这应该有效:

import asyncstdlib

async for i, numbers in asyncstdlib.enumerate(gen_random_numbers()):
    print(f"working with the batch {i}  and processing {numbers}")
© www.soinside.com 2019 - 2024. All rights reserved.