Skip to main content

jetblack-memoryqueue

This is a Python >=3.11 project which implements a "memory queue".

The memory queue is a container that presents chunks of data as a contiguous sequence.

This can be useful when parsing streaming data. Typically the data is read in blocks of equal size until the token is found. This leads to a lot of memory allocation.

data = b''

...

data += read_block() # memory allocation
i = data.index(b`\n\r')
if i != -1:
    line = data[:i+2] # memory allocation
    data = data[i+2:] # memory allocation

With a memory queue each block is wrapped in a memoryview and appended to an internal queue.

data = memoryqueue

...

data += read_block() # wrapped in memoryview and appended to internal queue.
i = data.index(b`\n\r')
if i != -1:
    line = bytes(data[:i+2]) # memory allocation
    data = data[i+2:] # could drop a memoryview if i+2 is more than it's length.

The means the memory allocation of the bytes only happens when requested with the bytes call.

Example

Here is an example of parsing an http request.

from typing import Iterator

from jetblack_memoryqueue import memoryqueue


def byte_stream(n: int) -> Iterator[bytes]:
    buf = b'\
POST /test HTTP/1.1\n\r\
Host: example.com\n\r\
Content-Type: multipart/form-data;boundary="delimiter12345"\n\r\
\n\r\
--delimiter12345\n\r\
Content-Disposition: form-data; name="field1"\n\r\
\n\r\
value1\n\r\
--delimiter12345\n\r\
Content-Disposition: form-data; name="field2"; filename="example.txt"\n\r\
\n\r\
value2\n\r\
--delimiter12345--'
    return (
        buf[0+i:n+i]
        for i in range(0, len(buf), n)
    )


def main() -> None:

    read_buf = iter(byte_stream(10))

    parts: list[bytes] = []

    # Use a memory queue to accumulate the data instead of a bytes object.
    # data = b''
    data = memoryqueue()

    while buf := next(read_buf, None):
        data += buf
        index = data.find(b"\n\r\n\r")
        if index >= 0:
            parts.append(bytes(data[:index]))
            data = data[index+4:]

    if len(data) > 0:
        parts.append(bytes(data))

    for part in parts:
        print(part)


if __name__ == "__main__":
    main()

Installation

The package can be installed from the pypi.

pip install jetblack-memoryqueue

Metadata

Release files for jetblack-memoryqueue 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for jetblack-memoryqueue 0.2.0
File Size Uploaded
jetblack_memoryqueue-0.2.0.tar.gz 17.3 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for jetblack-memoryqueue 0.2.0
File
jetblack_memoryqueue-0.2.0-cp314-cp314t-manylinux1_x86_64.manylinux_2_5_x86_64.whl CPython 3.14 CPython 3.14 free-threading Linux glibc 2.5+ x86-64 Details
jetblack_memoryqueue-0.2.0-cp314-cp314-manylinux1_x86_64.manylinux_2_5_x86_64.whl CPython 3.14 CPython 3.14 Linux glibc 2.5+ x86-64 Details
jetblack_memoryqueue-0.2.0-cp313-cp313-manylinux1_x86_64.manylinux_2_5_x86_64.whl CPython 3.13 CPython 3.13 Linux glibc 2.5+ x86-64 Details
jetblack_memoryqueue-0.2.0-cp312-cp312-manylinux1_x86_64.manylinux_2_5_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.5+ x86-64 Details
jetblack_memoryqueue-0.2.0-cp311-cp311-manylinux1_x86_64.manylinux_2_5_x86_64.whl CPython 3.11 CPython 3.11 Linux glibc 2.5+ x86-64 Details

Total release size: 354.5 kB

Release files / jetblack_memoryqueue-0.2.0.tar.gz

Download URL jetblack_memoryqueue-0.2.0.tar.gz
Size 17.3 kB
Tags Source
SHA-256 checksum
How to use checksums
2dff7d5366f8a90d49fffdb0170c764562042e9f7bf74c175109b3519e21176a
BLAKE2b-256 checksum
How to use checksums
0d609f8dcf208ed1018ea90a5ba885e83b36f1e047ef66230ff400779d3af2ed
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13+

Release files / jetblack_memoryqueue-0.2.0-cp314-cp314t-manylinux1_x86_64.manylinux_2_5_x86_64.whl

Download URL jetblack_memoryqueue-0.2.0-cp314-cp314t-manylinux1_x86_64.manylinux_2_5_x86_64.whl
Size 77.3 kB
Tags CPython 3.14 CPython 3.14 free-threading Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
c7071b14f57d5fb8e7287e82b3bc3e8e06dc1180560737d5ae6461cbbdaf8002
BLAKE2b-256 checksum
How to use checksums
0fafdead5f8552b5749167e8c61ce73dc1bf734fe59ce7cad7518015308133e2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13+

Release files / jetblack_memoryqueue-0.2.0-cp314-cp314-manylinux1_x86_64.manylinux_2_5_x86_64.whl

Download URL jetblack_memoryqueue-0.2.0-cp314-cp314-manylinux1_x86_64.manylinux_2_5_x86_64.whl
Size 66.0 kB
Tags CPython 3.14 Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
1daee77450a4027e549c8e525559472306e2159377480bc9d06c4177e9ab7277
BLAKE2b-256 checksum
How to use checksums
83c78c5478e5f94d2f961481a329e08d4895142059cad30b559ca5bdd75eadb1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13+

Release files / jetblack_memoryqueue-0.2.0-cp313-cp313-manylinux1_x86_64.manylinux_2_5_x86_64.whl

Download URL jetblack_memoryqueue-0.2.0-cp313-cp313-manylinux1_x86_64.manylinux_2_5_x86_64.whl
Size 65.5 kB
Tags CPython 3.13 Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
4faf91c838fcdc78cd1b929d7f097c1ac292ca781d24ffdc92b490a08c1d8483
BLAKE2b-256 checksum
How to use checksums
27b46f4d2830648a461cb5925d34a0ebf5e9b4584c166cf66a8578f7db09325b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13+

Release files / jetblack_memoryqueue-0.2.0-cp312-cp312-manylinux1_x86_64.manylinux_2_5_x86_64.whl

Download URL jetblack_memoryqueue-0.2.0-cp312-cp312-manylinux1_x86_64.manylinux_2_5_x86_64.whl
Size 65.5 kB
Tags CPython 3.12 Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
d64b269e34e065ad96ea6c835b4c80dfb9dc87cd28e7c6473b96d8f81c4b7f75
BLAKE2b-256 checksum
How to use checksums
8f421b3216101ef2dc49ffd91231ac200af5eb52addf1713d18f5cc93f39d783
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13+

Release files / jetblack_memoryqueue-0.2.0-cp311-cp311-manylinux1_x86_64.manylinux_2_5_x86_64.whl

Download URL jetblack_memoryqueue-0.2.0-cp311-cp311-manylinux1_x86_64.manylinux_2_5_x86_64.whl
Size 62.9 kB
Tags CPython 3.11 Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
eb2749fda19c953877205496e922446edd04ba94e07e1de55ce254c56a95a5ef
BLAKE2b-256 checksum
How to use checksums
2f092ca1d2858ce7d801602a9cb1038c21d822ededbd87fd67ffdd7e477ac732
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13+

Release history Release notifications | RSS feed

This release

0.2.0 This release

6 release files

0.1.0

6 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page