Solution to set stream response with aiohttp #2012
-
Hi everyone, i got some problems when i want to download a file from some api and save it to import aiohttp
class StreamDownload:
async def on_get(self, req, resp)
async with aiohttp.ClientSession() as session:
async with session.get('api') as response:
resp.stream = response.content.iter_chunked(1024)
async for data in response.content.iter_chunked(1024):
yield data My problem is when the framework read from In falcon.app: ...
stream = resp.stream
...
try:
async for data in stream:
# The connection has been closed, so it will raise some aiohttp.Connection error
... Is there any solution to deal with this situation? Thanks a lot! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @icebearrrr ! As to your import aiohttp
import falcon.asgi
class StreamDownload:
LOGO = 'https://falcon.readthedocs.io/en/stable/_static/img/logo.svg'
async def aiohttp_stream(self, uri):
async with aiohttp.ClientSession() as session:
async with session.get(uri) as response:
# NOTE(vytas): Cannot use `yield from` here in async code.
async for chunk in response.content.iter_chunked(1024):
yield chunk
async def on_get(self, req, resp):
resp.content_type = 'image/svg+xml'
resp.stream = self.aiohttp_stream(self.LOGO)
app = falcon.asgi.App()
app.add_route('/logo.svg', StreamDownload()) My |
Beta Was this translation helpful? Give feedback.
Hi @icebearrrr !
One way is to use an HTTP client library's functionality that is not based on context managers, if the library in question supports such (I'm not sure if this is possible with
aiohttp
though). The following Gist demonstrates how to manually close a connection using another popular client,httpx
: Proxying a website with Falcon ASGI + httpx.As to your
aiohttp
snippet, maybe you could simply wrap it in anotherasync
generator, and assign that toresp.stream
?Something along the lines of (not tested in depth!)