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.2.tar.gz (13.9 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.2-cp312-cp312-win_amd64.whl (246.0 kB view details)

Uploaded CPython 3.12Windows x86-64

libgossip-0.2.2-cp312-cp312-musllinux_1_1_x86_64.whl (937.4 kB view details)

Uploaded CPython 3.12musllinux: musl 1.1+ x86-64

libgossip-0.2.2-cp312-cp312-musllinux_1_1_i686.whl (1.0 MB view details)

Uploaded CPython 3.12musllinux: musl 1.1+ i686

libgossip-0.2.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (422.9 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

libgossip-0.2.2-cp311-cp311-win_amd64.whl (244.1 kB view details)

Uploaded CPython 3.11Windows x86-64

libgossip-0.2.2-cp311-cp311-musllinux_1_1_x86_64.whl (936.3 kB view details)

Uploaded CPython 3.11musllinux: musl 1.1+ x86-64

libgossip-0.2.2-cp311-cp311-musllinux_1_1_i686.whl (1.0 MB view details)

Uploaded CPython 3.11musllinux: musl 1.1+ i686

libgossip-0.2.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (422.9 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

libgossip-0.2.2-cp310-cp310-win_amd64.whl (243.7 kB view details)

Uploaded CPython 3.10Windows x86-64

libgossip-0.2.2-cp310-cp310-musllinux_1_1_x86_64.whl (935.4 kB view details)

Uploaded CPython 3.10musllinux: musl 1.1+ x86-64

libgossip-0.2.2-cp310-cp310-musllinux_1_1_i686.whl (1.0 MB view details)

Uploaded CPython 3.10musllinux: musl 1.1+ i686

libgossip-0.2.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (422.3 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

libgossip-0.2.2-cp39-cp39-win_amd64.whl (253.7 kB view details)

Uploaded CPython 3.9Windows x86-64

libgossip-0.2.2-cp39-cp39-musllinux_1_1_x86_64.whl (935.7 kB view details)

Uploaded CPython 3.9musllinux: musl 1.1+ x86-64

libgossip-0.2.2-cp39-cp39-musllinux_1_1_i686.whl (1.0 MB view details)

Uploaded CPython 3.9musllinux: musl 1.1+ i686

libgossip-0.2.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (422.2 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

libgossip-0.2.2-cp38-cp38-win_amd64.whl (243.4 kB view details)

Uploaded CPython 3.8Windows x86-64

libgossip-0.2.2-cp38-cp38-musllinux_1_1_x86_64.whl (935.5 kB view details)

Uploaded CPython 3.8musllinux: musl 1.1+ x86-64

libgossip-0.2.2-cp38-cp38-musllinux_1_1_i686.whl (1.0 MB view details)

Uploaded CPython 3.8musllinux: musl 1.1+ i686

libgossip-0.2.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (421.8 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

File details

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

File metadata

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

File hashes

Hashes for libgossip-0.2.2.tar.gz
Algorithm Hash digest
SHA256 f7dc49378b040a171112507f5a2e8beece3973c7ee7431102b2eff94ecd75738
MD5 f27dbe86bd40d4fa73b10ca2ef1b11bc
BLAKE2b-256 ff5aee6915edc555b9336c8d31bfca3b1ea682bc77d5bd7e4d1663c72f367a05

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for libgossip-0.2.2-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 4a4426e381c45032969f80e361e94ce90c92f3da253d66bf42dbb459966ec5f1
MD5 b41b8ba3e2d58e85d112b3e488ed3986
BLAKE2b-256 c8e9a1be114f1118b685d59896f6e1c9af70d53b92a00c312aa42681968ce469

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libgossip-0.2.2-cp312-cp312-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 3a6db772fa01282a9185fadcd9cc360f16d416aef7879664b14ca9ec3ae1f55e
MD5 0a77633e1a213882bd32a78e1ead2eb4
BLAKE2b-256 1acf28f1b65e6f748dfab64d1c7f1dddf7d9d65f03674ae0d3dd670752f1f216

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libgossip-0.2.2-cp312-cp312-musllinux_1_1_i686.whl
Algorithm Hash digest
SHA256 bff74ecdad1ad42c5ec96afe555e91bb302a19ab5049891a5d437b28f8abd5f3
MD5 9dade26fa4daeb1dc72a40bfb5659d6c
BLAKE2b-256 cee73241056f78575a4d73a4f91516a37baff1d3c62ce808c4182c119a55d4f1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libgossip-0.2.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c4b50d94678f18e1a940756f1e97fcb38ad2e1845a9b70725160e6c0c95f4a20
MD5 bfdf3ac0c4d0663083940cf39f57bf3a
BLAKE2b-256 d30ebe5889ec3744a26cb65ef3e3ea5f9c1472cb78749692c85e0cdb06411b60

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for libgossip-0.2.2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 bc137d16fef396f8279254bce7be80cd5e80adb8dba1703397a56c4bfce4dbd8
MD5 39ef9299534a164bf08060a68e720ef7
BLAKE2b-256 093aa6427ef4768bfa7f9f566ae78c543b27cd9f070587066fd5a93eac501413

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libgossip-0.2.2-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 b6fd5536093ab2ef1c9974d4276a07e2e53f170db937df90372a756cd84c850a
MD5 f9449b3fd3f7eea5501b52d783e49541
BLAKE2b-256 0f01a2b1825dba9f65b616b1a7811faa823004bdb1a04e6a3410f879d737a272

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libgossip-0.2.2-cp311-cp311-musllinux_1_1_i686.whl
Algorithm Hash digest
SHA256 ca58eaf84152f070ea10f8f1c100cc4bb0327691348858af5fbd754ec8355f72
MD5 dce47d22a45d3baff5afe65b74eb8561
BLAKE2b-256 9307d7d68c3e8794fff29a7a83991226233d7179ed9334ee1ff64611eddb3c9d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libgossip-0.2.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 25889f47c1ad490e614c0539a51bb043ea66ac5d3a3aa7148604bcbbe4ef3ba3
MD5 7189821b34644c001d926d58a0654349
BLAKE2b-256 eff09103b02cae9216389c4fe6e96b8f1b26ca6a1d2dddce822bd56126de1f3c

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for libgossip-0.2.2-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 a2d996e22b6cf19688f17be6bd5e7d8842daff074d3599eced8978852c8d3b5a
MD5 991d41c2f34af4ea0afa708d373da718
BLAKE2b-256 3bf3725112c8876cb7f3292344722b86e5189c1081e1c915317122e0aac8a31e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libgossip-0.2.2-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 46ea00b51d531d85ca964ffeb0e4cf7bfe390c7e05256f1d302ace0b927c4d4a
MD5 b276e52b69a44962e5906a9481b725f9
BLAKE2b-256 a001ae200e92f16012055cb9b15755d1004bf7636a73fca1bdd7da552ac26b55

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libgossip-0.2.2-cp310-cp310-musllinux_1_1_i686.whl
Algorithm Hash digest
SHA256 e33e296ef3b0598f57ac2321932b9b02327de5f8c96f29779ca8057ce9cc1ef4
MD5 fc9fed09ffb2e49f4ed5b12c28741b05
BLAKE2b-256 b47d9f344ea666d9650a41ce3eb1421a47225bf56de33cea653675849bcf3c3c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libgossip-0.2.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 aad70bc64f9ce5ba49efb486d57400d35e65cb44c50d8a71a35aa0f05e1a8dd3
MD5 c016484829ac77679e93c77c9d09999f
BLAKE2b-256 71651701be9e3deecbb7d0812f1dd3d31a9aa1b0286888dcc835cd352dd8f6c1

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for libgossip-0.2.2-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 e3ba7bfda0e95db893ef4c42846bb2bf3a3c1ffee7302e6d48cf7b874e34fa79
MD5 1b18a96acd117bebb63a5e498ee287e9
BLAKE2b-256 62caa98edb748534964285da158954c05f3e939cfc5e9dad3db2f7d0219ba41b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libgossip-0.2.2-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 df4352a1e8af24256be93dbd055b831be2a48aacd86b8cca255ecc638cc0152e
MD5 3dd7ee3e9cf23192917522588a1c41ab
BLAKE2b-256 4540c9de0611a9a80e5b20194cba4e3119b0d753b2ca1fd96f808e6babb378a5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libgossip-0.2.2-cp39-cp39-musllinux_1_1_i686.whl
Algorithm Hash digest
SHA256 1b7fd483441e84625f7c5e6a0fb7878f3a3e006c569759834ffc1edd991b4b3b
MD5 f0c97f0e83575d4a7d680368a0d8b124
BLAKE2b-256 e4cae1c4bdfc7d9a3ca242ef56ffb293b7c6364bd7e2c7942da5c2deaafeac30

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libgossip-0.2.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 cb1e80ef7c4a6926be80ca364dc82e1284d48d1e138b5077b30777293a6e19e6
MD5 376ba9f739cea285f0556e251d51f0c6
BLAKE2b-256 c9a9170394de13f6298f5dc872fc5fba792ccb4b194f21b2f5bafae252f7bcb3

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for libgossip-0.2.2-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 641781ae579cb6d0c1f7007171b8ed4779888cf3389b0ba5596660282c927405
MD5 6a1251780be7f6d4800e579b4bf087dd
BLAKE2b-256 74e245aa8227e466aacab3d40bb3ae543ea0a1dab21c87d2eaa6aca745223ed8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libgossip-0.2.2-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 99935aa456777e80544171bbaacea08f1abc2d7d11e34dc4f039d4085a81c8d1
MD5 083a0c4003c46ab020d1d6faf7ec299c
BLAKE2b-256 3cab28ded239ca28d8de99d1cbc814de37e9037fb7df274cb4fc839f7661da69

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libgossip-0.2.2-cp38-cp38-musllinux_1_1_i686.whl
Algorithm Hash digest
SHA256 512e09f97123e82021bb2a6090b89cb6ebae79beb5daee9fd7a767c0c936b1af
MD5 a6e8d95f288e4604f4a4abd11850ad3d
BLAKE2b-256 0aa0fa63934aa20a9f3c994ce1f9097828e882c936031b88ba3302acf2e29520

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libgossip-0.2.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ff1b6ecdb332aefe2de1aef52b81b8d3977c939d3c11ea5f31f414fefbbec481
MD5 5c3067988f5baa5cacd4ffee7de1b00b
BLAKE2b-256 0cf09e24ca9a3f4aa6fd44a600f9f4ca10fb3c30ecca9365d48d7a5170141672

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