Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage with smart open #187

Open
andrewkruse opened this issue Jun 28, 2024 · 1 comment
Open

Usage with smart open #187

andrewkruse opened this issue Jun 28, 2024 · 1 comment

Comments

@andrewkruse
Copy link

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
Copy link
Owner

Tinche commented Jun 28, 2024

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants