Skip to main content

Python bindings for libgossip - a C++ Gossip protocol implementation

Project description

libgossip Python Bindings

This package provides Python bindings for the libgossip C++ library, which implements the Gossip protocol for decentralized distributed systems.

Installation

For regular usage, install the package from PyPI:

pip install libgossip

Then use it in your Python code:

import libgossip
# Use the library

Building from Source

To build the package from source, you need to have CMake and a C++17 compatible compiler installed.

git clone https://github.com/caomengxuan666/libgossip.git
cd libgossip
git submodule update --init
mkdir build
cd build
cmake .. -DBUILD_PYTHON_BINDINGS=ON
cmake --build .

Running Examples

For Users (After Installation)

After installing the package with pip install libgossip, you can run examples from anywhere:

python example.py

For Developers (Using Built Source)

The examples can be run directly from the bindings/python directory after building the project:

# Build the project first (as shown above)
# Then run examples from the bindings/python directory:

# Method 1: Set PYTHONPATH and run directly
set PYTHONPATH=.
python example.py

# Method 2: Run with python -m
python -m libgossip.example

# Method 3: Run examples from within the libgossip package directory
cd libgossip
python sdk_example.py
python decorator_example.py
python advanced_decorator_example.py
cd ..

Note: Examples must be run from the bindings/python directory to work correctly with the built module.

Usage

Low-level API

import libgossip

# Create a node view for ourself
self_node = libgossip.NodeView()
self_node.ip = "127.0.0.1"
self_node.port = 7000
self_node.status = libgossip.NodeStatus.ONLINE

# Define callbacks
def send_callback(msg, target):
    print(f"Sending message of type {msg.type} to {target.ip}:{target.port}")

def event_callback(node, old_status):
    print(f"Node {node.ip}:{node.port} changed from {old_status} to {node.status}")

# Initialize gossip core
core = libgossip.GossipCore(self_node, send_callback, event_callback)

# Meet another node
other_node = libgossip.NodeView()
other_node.ip = "127.0.0.1"
other_node.port = 7001
other_node.status = libgossip.NodeStatus.JOINING

core.meet(other_node)

High-level API

import libgossip

# Create and start a node using the high-level API
with libgossip.create_node("127.0.0.1", 7000) as node:
    # Register handlers using decorators
    @node.on_message
    def log_message(msg, target):
        print(f"[SEND] {msg.type} to {target.ip}:{target.port}")
        
    @node.on_event
    def log_event(node_view, old_status):
        print(f"[EVENT] {node_view.ip}:{node_view.port} changed from {old_status} to {node_view.status}")
        
    # Create another node to meet
    other_node = libgossip.create_node("127.0.0.1", 7001)
    other_node.start()
    
    # Meet the other node
    node.meet(other_node)
    
    # Run gossip protocol for a few ticks
    for i in range(5):
        node.tick()

API Reference

The package provides both low-level and high-level APIs:

Low-level Classes

  • GossipCore - Main protocol implementation
  • NodeView - Node representation with metadata
  • GossipMessage - Message structure for network transport
  • NodeId - Node unique identifier
  • NodeStatus - Node status enumeration
  • MessageType - Message type enumeration
  • GossipStats - Statistics about the gossip protocol

High-level Classes

  • GossipNode - High-level wrapper for a gossip node
  • create_node() - Convenience function to create a GossipNode
  • create_cluster() - Convenience function to create a cluster of nodes

Decorators

libgossip provides a rich set of decorators to simplify network programming:

Basic Decorators

  • @message_handler - Decorator for message handlers
  • @event_handler - Decorator for event handlers
  • @node.on_message - Method decorator for message handlers
  • @node.on_event - Method decorator for event handlers

Filtering Decorators

  • @message_type_filter(*message_types) - Filter message handlers by message type
  • @node_status_monitor(*statuses) - Filter event handlers by node status changes

Robustness Decorators

  • @retry_on_network_error(max_retries=3, delay=0.1) - Automatically retry network operations on failure
  • @circuit_breaker(max_failures=3, timeout=60) - Circuit breaker pattern for network operations
  • @with_timeout(timeout=5.0) - Add timeout to network operations

Performance Decorators

  • @rate_limit(calls_per_second=1) - Rate limit network operations
  • @measure_latency - Measure network operation latency
  • @async_network_operation - Run network operations asynchronously

Lifecycle Decorators

  • @node_lifecycle(auto_start=True, auto_stop=True) - Automatically manage node lifecycle

Cluster Decorators

  • @broadcast_to_cluster(exclude_self=True) - Automatically broadcast messages to all nodes in cluster

Example Usage of Decorators

import libgossip

# Filter messages by type
@libgossip.message_type_filter(libgossip.MessageType.PING, libgossip.MessageType.PONG)
def handle_ping_pong(msg, target):
    print(f"Handling {msg.type} from {target.ip}:{target.port}")

# Monitor specific node status changes
@libgossip.node_status_monitor(libgossip.NodeStatus.ONLINE, libgossip.NodeStatus.FAILED)
def handle_status_change(node, old_status):
    print(f"Node {node.ip}:{node.port} changed from {old_status} to {node.status}")

# Add retry logic to network operations
@libgossip.retry_on_network_error(max_retries=3, delay=0.5)
def robust_send(node, msg, target):
    return node.send_message(msg, target)

# Rate limit operations
@libgossip.rate_limit(calls_per_second=10)
def frequent_operation(node):
    # This will be limited to 10 calls per second
    pass

# Add circuit breaker protection
@libgossip.circuit_breaker(max_failures=5, timeout=30)
def protected_network_call(node, msg, target):
    return node.send_message(msg, target)

# Measure operation latency
@libgossip.measure_latency
def timed_operation(node):
    node.tick()

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

libgossip-0.2.0.tar.gz (13.6 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

libgossip-0.2.0-cp312-cp312-win_amd64.whl (202.1 kB view details)

Uploaded CPython 3.12Windows x86-64

libgossip-0.2.0-cp312-cp312-musllinux_1_1_x86_64.whl (873.6 kB view details)

Uploaded CPython 3.12musllinux: musl 1.1+ x86-64

libgossip-0.2.0-cp312-cp312-musllinux_1_1_i686.whl (945.0 kB view details)

Uploaded CPython 3.12musllinux: musl 1.1+ i686

libgossip-0.2.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (353.5 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

libgossip-0.2.0-cp311-cp311-win_amd64.whl (200.7 kB view details)

Uploaded CPython 3.11Windows x86-64

libgossip-0.2.0-cp311-cp311-musllinux_1_1_x86_64.whl (872.0 kB view details)

Uploaded CPython 3.11musllinux: musl 1.1+ x86-64

libgossip-0.2.0-cp311-cp311-musllinux_1_1_i686.whl (944.0 kB view details)

Uploaded CPython 3.11musllinux: musl 1.1+ i686

libgossip-0.2.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (353.6 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

libgossip-0.2.0-cp310-cp310-win_amd64.whl (200.1 kB view details)

Uploaded CPython 3.10Windows x86-64

libgossip-0.2.0-cp310-cp310-musllinux_1_1_x86_64.whl (871.0 kB view details)

Uploaded CPython 3.10musllinux: musl 1.1+ x86-64

libgossip-0.2.0-cp310-cp310-musllinux_1_1_i686.whl (943.4 kB view details)

Uploaded CPython 3.10musllinux: musl 1.1+ i686

libgossip-0.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (353.1 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

libgossip-0.2.0-cp39-cp39-win_amd64.whl (209.5 kB view details)

Uploaded CPython 3.9Windows x86-64

libgossip-0.2.0-cp39-cp39-musllinux_1_1_x86_64.whl (871.2 kB view details)

Uploaded CPython 3.9musllinux: musl 1.1+ x86-64

libgossip-0.2.0-cp39-cp39-musllinux_1_1_i686.whl (944.2 kB view details)

Uploaded CPython 3.9musllinux: musl 1.1+ i686

libgossip-0.2.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (353.1 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

libgossip-0.2.0-cp38-cp38-win_amd64.whl (199.6 kB view details)

Uploaded CPython 3.8Windows x86-64

libgossip-0.2.0-cp38-cp38-musllinux_1_1_x86_64.whl (870.9 kB view details)

Uploaded CPython 3.8musllinux: musl 1.1+ x86-64

libgossip-0.2.0-cp38-cp38-musllinux_1_1_i686.whl (942.6 kB view details)

Uploaded CPython 3.8musllinux: musl 1.1+ i686

libgossip-0.2.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (352.4 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

File details

Details for the file libgossip-0.2.0.tar.gz.

File metadata

  • Download URL: libgossip-0.2.0.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for libgossip-0.2.0.tar.gz
Algorithm Hash digest
SHA256 4550d5bcb6567757a8cca9c88de21225fa4691d03541a4bb873a708a4b93eeb9
MD5 1a6c0fd7ff7393fed4480ffcce1300c8
BLAKE2b-256 b5021c4e91f34ca52689591cdb59b8580cb1e03bb7b4dd31b6db87cca4235b51

See more details on using hashes here.

File details

Details for the file libgossip-0.2.0-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: libgossip-0.2.0-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 202.1 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for libgossip-0.2.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 bf78a8e6ffce6ef22b68553dd932a3f57ce6493946f81c699d2d87a700eec079
MD5 b0c3a4cdb68753650479abfa3cb8f41b
BLAKE2b-256 4ef3642b2b25bdf86ff441bb602900f8c709bb21567a65d8e741a4f966a3800a

See more details on using hashes here.

File details

Details for the file libgossip-0.2.0-cp312-cp312-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for libgossip-0.2.0-cp312-cp312-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 b4ba80702ceb1c9582a223f88b6dc5b0499963c805c1d5fb60f1e0c84b0be995
MD5 44d9b9bebd2c697a80e95cc57280daaa
BLAKE2b-256 4464f479556a279f77d40de45b95fe77cdbd710244d30556bf1626bad79495cc

See more details on using hashes here.

File details

Details for the file libgossip-0.2.0-cp312-cp312-musllinux_1_1_i686.whl.

File metadata

File hashes

Hashes for libgossip-0.2.0-cp312-cp312-musllinux_1_1_i686.whl
Algorithm Hash digest
SHA256 165701e47f80e58e1cc159b6be9e32548bddda9eabda99d52621595aeb3ee47d
MD5 2a1450261f05e9c2d39d28cd4d43dd9d
BLAKE2b-256 5c91fb1a6cad4820da9cd77f6e5bc4c7cf10691159ac57d047f35e055ad3e40f

See more details on using hashes here.

File details

Details for the file libgossip-0.2.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for libgossip-0.2.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b3740c208b90c93a5b6e59917af46e830833636d64a2d6d689d24a6494869cb8
MD5 1520c08950baa7f75759c702712aca02
BLAKE2b-256 c0cb2030123bbae69f462f6361658731f16ee777c13910abdac3443e60f1dd60

See more details on using hashes here.

File details

Details for the file libgossip-0.2.0-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: libgossip-0.2.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 200.7 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for libgossip-0.2.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 ca8592ebd02c8b4c49bc847d2ced5f004157270db8eef8d4c66847c8f3eebbd9
MD5 bb84ae77ecb54891136bb5ca87537e01
BLAKE2b-256 c07b5d6a2c492ac7d4f12a6306aaf32c0d7dd74fdb72691a8b04d2036502b2cf

See more details on using hashes here.

File details

Details for the file libgossip-0.2.0-cp311-cp311-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for libgossip-0.2.0-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 194c97c6f1be3eca55499899faf36c6957dcc280bb06055b68321ade6d7f0236
MD5 ffe01eb2c168aa0a0ee278384d581976
BLAKE2b-256 566e0bf37ce21cf706341670699452acfdab4fad7c892a9dc56f3e7cb863568c

See more details on using hashes here.

File details

Details for the file libgossip-0.2.0-cp311-cp311-musllinux_1_1_i686.whl.

File metadata

File hashes

Hashes for libgossip-0.2.0-cp311-cp311-musllinux_1_1_i686.whl
Algorithm Hash digest
SHA256 b2a0272eba18a21c5828a9b76c0a63598abb7271c2a4be83a3568d31ffbbeaf7
MD5 f294d11bafaad1aee291141a8e35061d
BLAKE2b-256 3564fbd1e3e1c5e761518eb798c3349b96c24d94ef8e7bfefb4f2a4136aa6777

See more details on using hashes here.

File details

Details for the file libgossip-0.2.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for libgossip-0.2.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 48305f9ade86261adb5136b93f7ad678a1f3b6cb0fe98fd508b096cb084ab08f
MD5 3af5b84297da5ae76e7b53344a878b33
BLAKE2b-256 e9abf9b6ae1ae03e21e75dcaabbcf43fc9099bc56a99d165241cac80de87c829

See more details on using hashes here.

File details

Details for the file libgossip-0.2.0-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: libgossip-0.2.0-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 200.1 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for libgossip-0.2.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 4a3e4e921ad2bcffbba7ad91c28ddbb24a3ac935ae1d057ab1763512454c99b1
MD5 51b7bb3fcce9765b0beb060306eb4253
BLAKE2b-256 776e13f9f0b8c443418e97c1bcb299aea140de768f8f685f92ab2e9f646a8eb9

See more details on using hashes here.

File details

Details for the file libgossip-0.2.0-cp310-cp310-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for libgossip-0.2.0-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 ebc57efbe1b2837ef84644ad0627d15fea8adddd94b827007f16e5596b7b7b1c
MD5 63c9bf884fc7bf3f50b655368ebd9a9a
BLAKE2b-256 c07d82bae944db42665e5368dd46f724dc2a65efc1225a9f0ae277e9651d18b9

See more details on using hashes here.

File details

Details for the file libgossip-0.2.0-cp310-cp310-musllinux_1_1_i686.whl.

File metadata

File hashes

Hashes for libgossip-0.2.0-cp310-cp310-musllinux_1_1_i686.whl
Algorithm Hash digest
SHA256 5fd86bc9968c89150656b16792bacb3b5e9313c19f12b4f13282ac5e0eca21d3
MD5 00bea90f0672119cc7c05611e3feebed
BLAKE2b-256 3ea672fcd3b2e4e4a24681358a6a05800f6be07501c8458cc92f37226032614d

See more details on using hashes here.

File details

Details for the file libgossip-0.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for libgossip-0.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 809103de0cf064a4b56b59848ab167ebad59d17f9767b66850e189cabee9277a
MD5 c08791c34ec406ffcf20a3e43440bc80
BLAKE2b-256 31796104d81b4c65b5df4cdbefb08f1155b788fe24dc67e39b23fb463cb43e4f

See more details on using hashes here.

File details

Details for the file libgossip-0.2.0-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: libgossip-0.2.0-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 209.5 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for libgossip-0.2.0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 5c144c37f2cfba2fa3a4b0fac97d9d13203ff4a7435bf01d02db82bd65a35f89
MD5 2ddc606fe966f55f4cf335586961c899
BLAKE2b-256 fa032a3441f57ac34de4be08b3ebb783c7354bdff71b376ee235b7656f165434

See more details on using hashes here.

File details

Details for the file libgossip-0.2.0-cp39-cp39-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for libgossip-0.2.0-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 1fb2f534a513886f66b1191f2d0910295c3bbf758e6f1a26090941ee03200682
MD5 76036bb33ed801902c6591998cc1b4c7
BLAKE2b-256 6473ecbd97fc7118e5535eaad742c31542f1e9424fa2c0a9009e67fa328e6c98

See more details on using hashes here.

File details

Details for the file libgossip-0.2.0-cp39-cp39-musllinux_1_1_i686.whl.

File metadata

File hashes

Hashes for libgossip-0.2.0-cp39-cp39-musllinux_1_1_i686.whl
Algorithm Hash digest
SHA256 3eef85712426ee459d4108a1a53d5ecd1c0a713aa845ef791407fe656b0747aa
MD5 5fca31e244b5e6a77b10d034a739c5fd
BLAKE2b-256 6c63cdeb292834eb028a573fc16dab84614f46a85f8f8abf74a995823c641287

See more details on using hashes here.

File details

Details for the file libgossip-0.2.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for libgossip-0.2.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ecd98740247d481599e5902f5f5b92fe26d747af130e6b472fded6435618e6bd
MD5 48e84015ce50bc0c99c0463894ff18ec
BLAKE2b-256 717991305b9e8a65845dd04eb08f20fc9abde4c8f3086e7e249c12670993cdd5

See more details on using hashes here.

File details

Details for the file libgossip-0.2.0-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: libgossip-0.2.0-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 199.6 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for libgossip-0.2.0-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 fffa59a9145ed88016e3fd067861ab542d125e4a01a22f782c1809ad7bd8fa7c
MD5 38077b4561b4f2bca139f38a106792be
BLAKE2b-256 bb9d020d6638aa579bfc6a1cc9ba557e74c3ba1ed4ba5bd4447a77f946637d45

See more details on using hashes here.

File details

Details for the file libgossip-0.2.0-cp38-cp38-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for libgossip-0.2.0-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 fbff719df7812d05bf51ae29b742fd3ce7ab02dc1f2e898d34f47a31cd1d3ac3
MD5 a5d9e7374eba6c569700c3ff34d31c43
BLAKE2b-256 71b504542d7504b8516124033bd551dc0af387dbce8390dd1ed8b9c5c7aee765

See more details on using hashes here.

File details

Details for the file libgossip-0.2.0-cp38-cp38-musllinux_1_1_i686.whl.

File metadata

File hashes

Hashes for libgossip-0.2.0-cp38-cp38-musllinux_1_1_i686.whl
Algorithm Hash digest
SHA256 f02be0e44e80723a070dd5145eb43b1dd208fe171f0c974cb0e892dfc351f953
MD5 a92ae92c2711618b46ce51e1bda5693d
BLAKE2b-256 369a6dab0562a6a679dcfe3fd531fd42623d60a194d27f22199735d9ccc17036

See more details on using hashes here.

File details

Details for the file libgossip-0.2.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for libgossip-0.2.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5f5492bbc0f89f27df44c2cad50bef295ddef713f676dc8f73c8eb24f70b1810
MD5 0d44962340549a793b2bb6edd1559d5a
BLAKE2b-256 7089c79d633d7f8d0143231fb870779cd91956683078a45bede2467be41d258f

See more details on using hashes here.

Supported by

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