Python3.10+ utilities to translate iterators of bytes into file-like objects
Project description
Ever wanted to stream large JSON objects with FastAPI
asynchrounously?
Look no more, iteriotools
is the solution you've been looking for!
It provides a RawIterIO
class which can wrap an iterator of bytes into a read-only file-like object.
Example
cd example
To run the example project's application:
pipenv run uvicorn --reload project:app
To demonstrate the impact of RawIterIO
on streaming large JSON objects, first emulate a "liveness probe" by running
the following:
while time curl --silent http://127.0.0.1:8000/health > /dev/null; do :; done
Then, while still having the probe running, emulate a few clients GET
-ing some large JSON object with:
(
trap -- 'kill 0' EXIT
for _ in {0..31}; do
while curl --silent http://127.0.0.1:8000/users/batched > /dev/null; do :; done &
done
wait
)
You should notice a significant increase in response time on the "liveness probe" (e.g. 0.003 seconds --> 25 seconds).
Now, with the version that utilizes RawIterIO
to stream JSON chunk by chunk:
(
trap -- 'kill 0' EXIT
for _ in {0..31}; do
while curl --silent http://127.0.0.1:8000/users/streamed > /dev/null; do :; done &
done
wait
)
You should still notice a significant increase in response time, but way less extreme (e.g. 0.003 seconds -> 2 seconds), and that numbers scales better with the size of the JSON object being streamed instead of being batched.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for iteriotools-0.0.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 766167e0e2ba8a20829fcbe159b381edc00bbfc0b857fe7e2b44f890a14b8b28 |
|
MD5 | b874d0f5cfadf50859818138f8a25893 |
|
BLAKE2b-256 | 6bdda69173dbf87aff127c84d7fd3c41b58e06a89745bfe80ad57131c6ba0e31 |