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

fastapi-streaming-example - perhaps creating a new thread can be avoided & instead use create_task #93

Open
ksachdeva opened this issue Nov 20, 2024 · 2 comments
Labels
enhancement New feature or request triage

Comments

@ksachdeva
Copy link

Expected Behaviour

minimize or eliminate thread creation if the entire stack is async

Current Behaviour

Thread(target=lambda: asyncio.run(start_generation(query, user_id, session_id, streamer_queue))).start()

Above line will spawn a new thread for every request.

Code snippet

https://github.com/awslabs/multi-agent-orchestrator/blob/da9563fe9e87343fcda8abe6303b1e1d4f36c943/examples/fast-api-streaming/main.py#L119

Possible Solution

What would be an issue if you do the following -

asyncio.create_task(start_generation(query, user_id, session_id, streamer_queue))

instead of spawning a new thread

Steps to Reproduce

See -

Thread(target=lambda: asyncio.run(start_generation(query, user_id, session_id, streamer_queue))).start()

@ksachdeva ksachdeva added the bug Something isn't working label Nov 20, 2024
@brnaba-aws
Copy link
Contributor

Hi @ksachdeva
Thanks for you contribution.
This is a code example, just to show dev how they might use it. People are free to take the code and modify it the way they want.

Thanks,
Anthony

@brnaba-aws brnaba-aws added enhancement New feature or request and removed bug Something isn't working labels Nov 22, 2024
@ksachdeva
Copy link
Author

I understand that it is an example code however you should consider following perspectives -

An experienced programmer -
Since you have used thread they will make an assumption that the underlying framework/library is not truly asynchronous and has blocking calls. Based on my reading of your library you have done a good job of keeping it asynchronous all the way. If my understanding is indeed correct then this example code will mislead the experienced programmers.

A data scientist or junior programmer -
They generally copy/paste the code from examples without considering the implications. Creating a thread for every single request ... this can be disastrous!!

I hope the above perspectives help understand why it is important to worry about even the example code.

Regards
Kapil

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request triage
Projects
None yet
Development

No branches or pull requests

2 participants