Skip to content

Usage with smart open #187

Description

@andrewkruse

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())

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions