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

Fixing streaming responses. #19

Merged
merged 2 commits into from
Jan 31, 2016
Merged

Conversation

cbrichford
Copy link

Closing the body stream now closes the underlying aiohttp reponse object.

This is an attempt to improve on the fix proposed in #18. The fix in this PR does not require streaming the entire response into memory. The FlowControlStreamReader instance coming from aiohttp is wrapped with a proxy that adds a close method ( which closes the underlying aiohttp request ) and proxies all other attributes to the FlowControlStreamReader instance.

This allows you to write code like this:

resp = yield from s3.get_object(Bucket='mybucket', Key='k')
stream = resp['Body']
try:
    chunk = yield from stream.read(s)
    while len(chunk) > 0:
      ...
      chunk = yield from stream.read(s)
finally:
  stream.close()

Closing the body stream closes the underlying aiohttp reponse object.
@jettify
Copy link
Member

jettify commented Jan 19, 2016

Looks good to me. I will play with your PR for testing purposes.

@thehesiod
Copy link
Collaborator

btw Chris and I were discussing for adding "await with" support to this solution so clients don't need to manually close the response. In fact I believe the underlying aiohttp ClientResponse class already supports this.

@jettify
Copy link
Member

jettify commented Jan 31, 2016

I am sorry for delay. I tested your code and it works as expected 👍 It took me a while to figure out why there are no disconnection errors. Seems like main reason is that response object is collected by gc as result we see disconnection error. If I disable gc version without fix works correctly.

@oselivanov @cbrichford thank you for tracking and fixing this issue!

jettify added a commit that referenced this pull request Jan 31, 2016
@jettify jettify merged commit a010d4a into aio-libs:master Jan 31, 2016
@oselivanov
Copy link

Thank you guys!

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

Successfully merging this pull request may close these issues.

4 participants