-
Notifications
You must be signed in to change notification settings - Fork 73
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
Issues in Streaming response from flask-smorest #664
Comments
Interesting. I've never achieved streaming with Flask but this is something I'd be happy to support. I can't tell out of my head why it doesn't work. I guess the response decorator is not happy about receiving a generator. That would explain the first case. Not sure about the second, though. I don't know when I'll have time to investigate this. If someone wants to give it a go, I suggest looking in Flask code to see how views returning generators are treated and adapt the response decorator code here. |
So I was finally able to resolve this issue. The root cause of the issue was the @blp.response decorator that was not accepting generator responses. Finally ended up removing the decorator from my code and manually serializing the results. Here's the final code-
It would be interesting to see if the blp.response decorator can handle generators. |
I'm encountering issues in streaming response in flask-smorest. I'm following the guidance here - https://flask.palletsprojects.com/en/2.3.x/patterns/streaming/ for streaming responses from my flask-smorest application. Below is the MRE version of my code. Say my application is fetching foreign exchange rates for the past 1000 days for any currency requested by the end user.
This is the version without using streaming. It works perfectly and returns a list of json responses-
When I run this, it successfully runs and returns a list of json responses on my browser, like-
Now, comes the part when I try streaming the responses. I make the below changes to my code-
This strangely returns a list of 50 empty json responses-
[{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}]
I also tried this, but with the same result i.e. list of empty json responses, but additionally flask-smorest gave me a "AssertionError: applications must write bytes". Seems like the werkzeug serving.py file was throwing issues.
My entire application is ready and this is the last bit that is giving issues. I want to stream the responses, and there is something in flask-smorest that is causing the issue. Would really appreciate your support. Thanks!
The text was updated successfully, but these errors were encountered: