SHM Reader
A general-purpose Python SDK for reading and monitoring shared memory segments.
Features
- List available shared memory segments
- Read data from shared memory segments
- Monitor shared memory segments for changes
- Register custom parsers for different data formats
- Command-line interface for quick access
Installation
pip install shm-reader
Usage
As a Library
from shm_reader import ShmReader
import struct
# Create a reader instance
reader = ShmReader()
# List available shared memory segments
segments = reader.list_segments()
for segment in segments:
print(f"{segment.name}: {segment.size} bytes")
# Register a custom parser for a specific segment type
def market_data_parser(data):
"""Parse market data in format: symbol(12s), price(d), volume(q), timestamp(d)"""
if len(data) < 28: # 12 + 8 + 8 + 8 bytes
raise ValueError("Not enough data for market format")
symbol_bytes, price, volume, timestamp = struct.unpack("12sdqd", data[:28])
symbol = symbol_bytes.decode('utf-8').strip('\x00')
return {
"symbol": symbol,
"price": price,
"volume": volume,
"timestamp": timestamp
}
# Register the parser with the SHM Reader
ShmReader.register_parser("market", market_data_parser)
# Read a specific segment
segment = reader.read_segment("market:btcusdt")
if segment.parsed_data:
print(f"Symbol: {segment.parsed_data['symbol']}")
print(f"Price: {segment.parsed_data['price']}")
# Monitor a segment for changes, checking the 'timestamp' field for updates
for updated_segment in reader.monitor_segment("market:btcusdt", interval=0.5, update_field="timestamp"):
print(f"Update: {updated_segment.parsed_data['symbol']} @ {updated_segment.parsed_data['price']}")
Using the Command Line
List available segments:
shm-reader --list
Read a segment:
shm-reader --read market:btcusdt
Monitor a segment:
shm-reader --monitor market:btcusdt --update-field timestamp --interval 0.5
Clear segments:
shm-reader --clear
Register custom parsers from a JSON file:
shm-reader --register-parsers parsers.json
Example parser definition file (parsers.json):
[
{
"type": "market",
"format": "12sdqd",
"fields": ["symbol", "price", "volume", "timestamp"]
},
{
"type": "order",
"format": "12sddq",
"fields": ["symbol", "price", "quantity", "order_id"]
}
]
Segment Type Convention
The SDK uses a naming convention for segments to identify their type:
- Segment names can be prefixed with a type identifier:
type:name - Example:
market:btcusdt,order:book,volatility:btcusdt - The segment type is used to select the appropriate parser
Built-in Parsers
The SDK includes a built-in parser for "volatility" type segments with the following format:
- timestamp (double, 8 bytes)
- price (double, 8 bytes)
- volatility_1s (double, 8 bytes)
- high_1s (double, 8 bytes)
- low_1s (double, 8 bytes)
- volume_1s (long long, 8 bytes)
- update_count (long long, 8 bytes)
License
MIT License
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Metadata
Release files for shm-reader 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| shm_reader-0.1.0.tar.gz | 8.4 kB | Without provenance | Details for shm_reader-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| shm_reader-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for shm_reader-0.1.0-py3-none-any.whl |
Total release size: 16.9 kB
Release files / shm_reader-0.1.0.tar.gz
| Download URL | shm_reader-0.1.0.tar.gz |
|---|---|
| Size | 8.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
90838e25ea1a740e7b9688f903613cf52211a80de1acd75330a93954d4db96ed
|
|
BLAKE2b-256 checksum How to use checksums |
335278f0a383a27fcbcdbad2956447dcec2666a598e47348542603ac83c669d5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.3
|
Release files / shm_reader-0.1.0-py3-none-any.whl
| Download URL | shm_reader-0.1.0-py3-none-any.whl |
|---|---|
| Size | 8.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
50c6ef58e92f42ae51c7c7d2edf028d3da952f3652b7bc7081f09b513d15c516
|
|
BLAKE2b-256 checksum How to use checksums |
5bb747f35f95b6684f4e126ce8b04abca48ac97152cbe683a214aa73d23f686a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.3
|
Security for shm-reader 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.