Tinche / aiofiles

File support for asyncio
Apache License 2.0
2.61k stars 151 forks source link

Usage with smart open #187

Open andrewkruse opened 1 week ago

andrewkruse commented 1 week ago

I am currently using fastapi to return a streaming response from results in google cloud storage. So, with fastapi, I have a few things that I use that make me want to use the async def options (asyncpg, aiohttp). But, if I do that my understanding is that as soon as I do blocking io in that same thread I'm going to get a stalled event loop.

As an option, I'm wondering if I can use this library and tack smart_open inside and expect it to continue working and not stall my event loop? Or perhaps there's a better/alternative way to patch smart open into this library in place of the regular open call?

Here's what I'm executing:

from smart_open import open
import aiofiles
aiofiles.threadpool.sync_open = open
async def download(request: Request) -> StreamingResponse:
    async def generate_response():
        async with aiofiles.open(f"gs://{bucket_name}/{file_name", 'rb') as f:
            while chunk := await f.read(2**16):
                yield chunk
    return StreamingResponse(generate_response())
Tinche commented 1 week ago

I wouldn't override aiofiles.threadpool.sync_open with smart_open, other modules in your codebase maybe depend on aiofiles (or will in the future) and that might mess them up. It's a very global change.

What aiofiles does is just run functions in a thread pool. Maybe you can run the smart_open stuff in a threadpool yourself?