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

StreamReader.read truncates content #3926

Closed
nkaretnikov opened this issue Jul 22, 2019 · 3 comments
Closed

StreamReader.read truncates content #3926

nkaretnikov opened this issue Jul 22, 2019 · 3 comments

Comments

@nkaretnikov
Copy link

Long story short

Performing a GET request. Switched from read() to content.read(n=NUM_BYTES) (the streaming API) in order to limit the number of bytes read. The content gets truncated: the number of bytes returned is less than NUM_BYTES.

Expected behavior

Read up to NUM_BYTES.

Actual behavior

The number of bytes returned is less than NUM_BYTES.

Steps to reproduce

Run this script:

import aiohttp
import asyncio

NUM_BYTES = 10_000_000  # 10 MB

async def f(url):
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as response:
            # content = await response.read()  # returns 22212
            content = await response.content.read(n=NUM_BYTES)  # returns 511
            print(len(content))


URL = 'https://upload.wikimedia.org/wikipedia/commons/thumb/7/71/2010-kodiak-bear-1.jpg/320px-2010-kodiak-bear-1.jpg'

asyncio.run(f(URL))

Your environment

  • macOS
  • Python 3.7.3
  • aiohttp 3.5.4

Related issues

@nkaretnikov
Copy link
Author

The number of read bytes varies (test.py is the above script):

% for i in `seq 1 50`; do python3 test.py; sleep 0.1; done | sort | uniq -c
   2 1810
   1 1811
   1 1813
  24 510
   6 511
   9 513
   4 514
   2 515
   1 5710

Could be related to my environment, let me know if you can reproduce. Or maybe it's just that I'm using the API incorrectly.

@socketpair
Copy link
Contributor

socketpair commented Jul 22, 2019

It's not a bug. It's by design. It reads no more than n bytes. It blocks (asynchronously) only if no data available at the moment.

@socketpair
Copy link
Contributor

@asvetlov, I had PR that fixes that issue. We can introduce parameter or another function like read_greedy(n).

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