Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

iteriotools-0.0.4.tar.gz (3.6 kB view hashes)

Uploaded Source

Built Distribution

iteriotools-0.0.4-py3-none-any.whl (3.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page