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.
Release files for iteriotools 0.0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| iteriotools-0.0.4.tar.gz | 3.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| iteriotools-0.0.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 7.2 kB
Release files / iteriotools-0.0.4.tar.gz
| Download URL | iteriotools-0.0.4.tar.gz |
|---|---|
| Size | 3.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c9ade6cb5adffca82d8bc83c4da9f930b4c43395f657d5826f30aac1b7f6b852
|
|
BLAKE2b-256 checksum How to use checksums |
c3312991f0166bf5a01e4a0a95ad433dfd00c4486ea01ab188dffa4f5660156a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
pdm/2.12.2 CPython/3.11.7
|
Release files / iteriotools-0.0.4-py3-none-any.whl
| Download URL | iteriotools-0.0.4-py3-none-any.whl |
|---|---|
| Size | 3.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
766167e0e2ba8a20829fcbe159b381edc00bbfc0b857fe7e2b44f890a14b8b28
|
|
BLAKE2b-256 checksum How to use checksums |
6bdda69173dbf87aff127c84d7fd3c41b58e06a89745bfe80ad57131c6ba0e31
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
pdm/2.12.2 CPython/3.11.7
|