Python client for SuhaibMessageQueue
Project description
PySMQ: Python Client for SuhaibMessageQueue
This package provides a Python client for interacting with SuhaibMessageQueue, a simple and efficient message queue service.
Installation
# From PyPI (once published)
pip install pysmq
# From source
git clone https://github.com/Suhaibinator/SuhaibMessageQueue.git
cd SuhaibMessageQueue
pip install -e ./pysmq
Dependencies
- Python 3.7+
- grpcio
- protobuf
Quick Start
Creating a client and publishing messages
from pysmq.client import Client
# Create a client
with Client(host="localhost", port=8097) as client:
# Create a topic
client.create_topic("my-topic")
# Publish a message
offset = client.produce("my-topic", b"Hello, SMQ!")
print(f"Message published at offset {offset}")
Consuming messages
from pysmq.client import Client
# Create a client
with Client(host="localhost", port=8097) as client:
# Consume the latest message
message, offset = client.consume_latest("my-topic")
print(f"Latest message: {message.decode('utf-8')}, offset: {offset}")
# Consume the earliest message
message, offset = client.consume_earliest("my-topic")
print(f"Earliest message: {message.decode('utf-8')}, offset: {offset}")
# Consume a specific message
message, offset = client.consume("my-topic", 5) # offset 5
print(f"Message at offset 5: {message.decode('utf-8')}")
Streaming messages
from pysmq.client import Client
# Create a client
with Client(host="localhost", port=8097) as client:
# Stream produce messages
messages = [b"Message 1", b"Message 2", b"Message 3"]
last_offset = client.stream_produce("my-topic", messages)
print(f"Last message published at offset {last_offset}")
# Stream consume messages (continuous)
for message, offset in client.stream_consume("my-topic", 0):
print(f"Received message: {message.decode('utf-8')}, offset: {offset}")
# Break the loop when needed
if offset >= 10:
break
Secure Connection (mTLS)
from pysmq.client import Client
from pysmq.config import ClientTLSConfig
# Create TLS configuration
tls_config = ClientTLSConfig(
cert_file="/path/to/client.crt",
key_file="/path/to/client.key",
ca_file="/path/to/ca.crt"
)
# Create a secure client
with Client(host="localhost", port=8097, tls_config=tls_config) as client:
# Use the client as normal
client.create_topic("secure-topic")
offset = client.produce("secure-topic", b"Secure message")
print(f"Secure message published at offset {offset}")
API Overview
Client
The Client class provides methods for interacting with the SMQ server.
# Constructor
client = Client(
host="localhost",
port=8097,
tls_config=None, # Optional: ClientTLSConfig for secure connections
max_send_message_size_mb=1024, # Optional: Maximum size of messages to send
max_receive_message_size_mb=1024 # Optional: Maximum size of messages to receive
)
Topic Management
# Create a topic
client.create_topic(topic)
Message Production
# Produce a single message
offset = client.produce(topic, message_bytes)
# Produce a stream of messages
last_offset = client.stream_produce(topic, messages_iterable)
Message Consumption
# Consume a single message at a specific offset
message, offset = client.consume(topic, offset)
# Consume the earliest available message
message, offset = client.consume_earliest(topic)
# Consume the latest message
message, offset = client.consume_latest(topic)
# Stream consume messages (returns an iterator)
for message, offset in client.stream_consume(topic, start_offset):
# Process each message
pass
Offset Management
# Get the latest offset for a topic
latest_offset = client.get_latest_offset(topic)
# Get the earliest available offset for a topic
earliest_offset = client.get_earliest_offset(topic)
# Delete all messages up to and including the specified offset
client.delete_until_offset(topic, offset)
Bulk Operations
# Retrieve a batch of messages
response = client.bulk_retrieve(topic, start_offset, limit)
for message in response.messages:
# Process each message
message_bytes = message.message
message_offset = message.offset
Connection Management
# Connect to the server (optional, happens automatically on first operation)
client.connect()
# Close the connection (or use 'with' statement)
client.close()
Exception Handling
The client throws specific exceptions for different types of errors:
from pysmq.exceptions import (
SMQException, # Base exception for all SMQ errors
SMQConnectionError, # Connection issues
SMQAuthenticationError, # TLS/authentication issues
SMQProduceError, # Issues with producing messages
SMQConsumeError, # Issues with consuming messages
SMQTopicCreationError, # Issues with creating topics
SMQOffsetError, # Issues with offsets
SMQTimeoutError, # Timeout errors
)
try:
client.produce("my-topic", b"Hello")
except SMQProduceError as e:
print(f"Failed to produce message: {e}")
# Access the original error if needed
original_error = e.original_error
Configuration
The client supports various configuration options:
from pysmq.config import ClientTLSConfig, ClientConfig
# Default timeout values can be accessed or modified
ClientConfig.DEFAULT_TIMEOUT = 60.0 # Default timeout for most operations
ClientConfig.DEFAULT_TOPIC_TIMEOUT = 2.0 # Default timeout for topic creation
ClientConfig.DEFAULT_DELETE_TIMEOUT = 2.0 # Default timeout for deletion operations
Examples
For more detailed examples, see the examples directory:
License
This project is licensed under the MIT License - see the LICENSE file for details.
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pysmq-1.0.7.tar.gz.
File metadata
- Download URL: pysmq-1.0.7.tar.gz
- Upload date:
- Size: 16.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ac3e59413aca35d48dcf27845fddc2dbd7566d57ab29620c44bd78278d445b19
|
|
| MD5 |
796f84185691a0e2ad002df199ab9634
|
|
| BLAKE2b-256 |
a65091f2c1b2a19fef36bdf88401eac39f41af327a53560658f538736d9b2683
|
Provenance
The following attestation bundles were made for pysmq-1.0.7.tar.gz:
Publisher:
publish-to-pypi.yml on Suhaibinator/SuhaibMessageQueue
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pysmq-1.0.7.tar.gz -
Subject digest:
ac3e59413aca35d48dcf27845fddc2dbd7566d57ab29620c44bd78278d445b19 - Sigstore transparency entry: 213255605
- Sigstore integration time:
-
Permalink:
Suhaibinator/SuhaibMessageQueue@dd6d6e29be984ba3200843137972921f6a14b330 -
Branch / Tag:
refs/tags/v1.0.7 - Owner: https://github.com/Suhaibinator
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@dd6d6e29be984ba3200843137972921f6a14b330 -
Trigger Event:
push
-
Statement type:
File details
Details for the file pysmq-1.0.7-py3-none-any.whl.
File metadata
- Download URL: pysmq-1.0.7-py3-none-any.whl
- Upload date:
- Size: 12.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ca0369a1d46bfbee5f8ec3e5cc5b1f205ba686a57371af4c312b8de989d6f6f8
|
|
| MD5 |
dd7f05bcfc3cfa45c716f35059aae2b4
|
|
| BLAKE2b-256 |
48b10947642bba9e68ce67c50944d1518db89ed03abbb38be0001ed8bd106cd1
|
Provenance
The following attestation bundles were made for pysmq-1.0.7-py3-none-any.whl:
Publisher:
publish-to-pypi.yml on Suhaibinator/SuhaibMessageQueue
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pysmq-1.0.7-py3-none-any.whl -
Subject digest:
ca0369a1d46bfbee5f8ec3e5cc5b1f205ba686a57371af4c312b8de989d6f6f8 - Sigstore transparency entry: 213255607
- Sigstore integration time:
-
Permalink:
Suhaibinator/SuhaibMessageQueue@dd6d6e29be984ba3200843137972921f6a14b330 -
Branch / Tag:
refs/tags/v1.0.7 - Owner: https://github.com/Suhaibinator
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@dd6d6e29be984ba3200843137972921f6a14b330 -
Trigger Event:
push
-
Statement type: