Python Flask TypeError: ‘async_generator’ object is not iterable

huangapple go评论100阅读模式
英文:

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' 对象不可迭代

我能知道问题在哪里以及如何修复吗?

编辑:

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

它使用了 for 关键字而不是 async for

英文:

i am trying to make a file streamer from telegram in flask

code i used

from flask import Response

@app.route(&quot;/dl/&lt;hash&gt;&quot;, methods=[&quot;GET&quot;])
async def download(hash):
  body = yield_file()
  return Response(
        status=206 if range_header else 200,
        response=body,
        headers={
            &quot;Content-Type&quot;: f&quot;{mime_type}&quot;,
            &quot;Content-Range&quot;: f&quot;bytes {from_bytes}-{until_bytes}/{file_size}&quot;,
            &quot;Content-Length&quot;: str(req_length),
            &quot;Content-Disposition&quot;: f&#39;{disposition}; filename=&quot;{file_name}&quot;&#39;,
            &quot;Accept-Ranges&quot;: &quot;bytes&quot;,
        },
    )

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] &quot;GET /dl/jRtYoKqEu6 HTTP/1.1&quot; 500 -
Error on request:
Traceback (most recent call last):
  File &quot;C:\Users\hp\AppData\Roaming\Python\Python310\site-packages\werkzeug\serving.py&quot;, line 364, in run_wsgi       
    execute(self.server.app)
  File &quot;C:\Users\hp\AppData\Roaming\Python\Python310\site-packages\werkzeug\serving.py&quot;, line 327, in execute        
    for data in application_iter:
  File &quot;C:\Users\hp\AppData\Roaming\Python\Python310\site-packages\werkzeug\wsgi.py&quot;, line 289, in __next__
    return self._next()
  File &quot;C:\Users\hp\AppData\Roaming\Python\Python310\site-packages\werkzeug\wrappers\response.py&quot;, line 32, in _iter_encoded
    for item in iterable:
TypeError: &#39;async_generator&#39; 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}")

huangapple
  • 本文由 发表于 2023年8月4日 01:03:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76830207.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定