-
What is the optimal chunk size for streaming response content? It's not indicated in the example from the official documentation: with open(filename, 'wb') as fd:
async for chunk in resp.content.iter_chunked(chunk_size):
fd.write(chunk) I'm currently using the chunk size of 1024. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 4 replies
-
I could be wrong, but I've always assumed the tradeoff is between performance and memory use. I have a backup program which needs to sync large amounts of data in one go (100MBs or GBs), for this I set the chunksize to 2MB (the library I was using defaulted to 1 byte and was incapable of exceeding 10KB/s due to that). For an embedded device with very limited memory, I imagine a chunksize of 1KB or less would be more appropriate. For the average application, I'm guessing around 1-10KB is safe. However, that's mostly my intuition, maybe somebody else with more knowledge can add some information. |
Beta Was this translation helpful? Give feedback.
-
I concur with @Dreamsorcerer Read buffer sizes for TCP Linux socket are: 4KB min, 85KB default, 4MB max. Linux dynamically adjusts the size. Values lesser than 4KB don't add much value from the performance point of view. The exact best number depends on your system and requires testing. |
Beta Was this translation helpful? Give feedback.
-
Thanks @Dreamsorcerer and @asvetlov |
Beta Was this translation helpful? Give feedback.
-
@asvetlov watching no performance improvement with larger chunks but found a bug: One example of the real chunk sizes for
|
Beta Was this translation helpful? Give feedback.
I concur with @Dreamsorcerer
Read buffer sizes for TCP Linux socket are: 4KB min, 85KB default, 4MB max. Linux dynamically adjusts the size.
Values lesser than 4KB don't add much value from the performance point of view.
4-8 (or even 16) MB can keep the read socket buffer empty and ready for new data packets.
The exact best number depends on your system and requires testing.