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.1.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.1-cp312-cp312-win_amd64.whl (208.9 kB view details)

Uploaded CPython 3.12Windows x86-64

libgossip-0.2.1-cp312-cp312-musllinux_1_1_x86_64.whl (885.2 kB view details)

Uploaded CPython 3.12musllinux: musl 1.1+ x86-64

libgossip-0.2.1-cp312-cp312-musllinux_1_1_i686.whl (959.1 kB view details)

Uploaded CPython 3.12musllinux: musl 1.1+ i686

libgossip-0.2.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (367.3 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

libgossip-0.2.1-cp311-cp311-win_amd64.whl (207.4 kB view details)

Uploaded CPython 3.11Windows x86-64

libgossip-0.2.1-cp311-cp311-musllinux_1_1_x86_64.whl (884.0 kB view details)

Uploaded CPython 3.11musllinux: musl 1.1+ x86-64

libgossip-0.2.1-cp311-cp311-musllinux_1_1_i686.whl (957.1 kB view details)

Uploaded CPython 3.11musllinux: musl 1.1+ i686

libgossip-0.2.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (367.1 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

libgossip-0.2.1-cp310-cp310-win_amd64.whl (206.8 kB view details)

Uploaded CPython 3.10Windows x86-64

libgossip-0.2.1-cp310-cp310-musllinux_1_1_x86_64.whl (883.2 kB view details)

Uploaded CPython 3.10musllinux: musl 1.1+ x86-64

libgossip-0.2.1-cp310-cp310-musllinux_1_1_i686.whl (956.4 kB view details)

Uploaded CPython 3.10musllinux: musl 1.1+ i686

libgossip-0.2.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (366.6 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

libgossip-0.2.1-cp39-cp39-win_amd64.whl (216.4 kB view details)

Uploaded CPython 3.9Windows x86-64

libgossip-0.2.1-cp39-cp39-musllinux_1_1_x86_64.whl (883.3 kB view details)

Uploaded CPython 3.9musllinux: musl 1.1+ x86-64

libgossip-0.2.1-cp39-cp39-musllinux_1_1_i686.whl (956.3 kB view details)

Uploaded CPython 3.9musllinux: musl 1.1+ i686

libgossip-0.2.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (366.4 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

libgossip-0.2.1-cp38-cp38-win_amd64.whl (206.6 kB view details)

Uploaded CPython 3.8Windows x86-64

libgossip-0.2.1-cp38-cp38-musllinux_1_1_x86_64.whl (883.3 kB view details)

Uploaded CPython 3.8musllinux: musl 1.1+ x86-64

libgossip-0.2.1-cp38-cp38-musllinux_1_1_i686.whl (955.5 kB view details)

Uploaded CPython 3.8musllinux: musl 1.1+ i686

libgossip-0.2.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (366.0 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

File details

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

File metadata

  • Download URL: libgossip-0.2.1.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.1.tar.gz
Algorithm Hash digest
SHA256 1f16e5cf79c521ccef58ea94d1a3b03bdb40a37777730fbf0b6152f8c3195b86
MD5 5d8bf0d20b0aa73186062c15bbc2888d
BLAKE2b-256 b3fdd88be5397e722e9b0fa49dc07eeb780304fe725f8d66a3fa66c87f49192d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: libgossip-0.2.1-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 208.9 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.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 7132af6331c8585615d85c085c271b416507ee851b4272cf0efeae28f7c04a5d
MD5 acb1e0eb09faa7c9675d6fffe46c1917
BLAKE2b-256 7b00ab0080c84eb973bf016d080f97d32b0876de6989ee2f6e7a0b3e8ee6f5cf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libgossip-0.2.1-cp312-cp312-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 30109a4b9f1d212ce22e866c27c310960ff7775bfda7681bf3dccf24b7a549ad
MD5 68026db7dfdfc569c20d1dc4dbcf76b0
BLAKE2b-256 bca5877529d66fe11f6a28e6dcd7a657d951ee1c153c14d9543faa2646db1405

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libgossip-0.2.1-cp312-cp312-musllinux_1_1_i686.whl
Algorithm Hash digest
SHA256 ffc96445b531278a291a6f8f3faa4390b2b6c66e1cdd6acfaa0dee91965572db
MD5 62fad05b1a0c8c5ef8238577ed1e37b8
BLAKE2b-256 586f9efb56212e0298e6dfb90bb0188bf125fa2bbaad3685fc0e3245dd24e53c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libgossip-0.2.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2b1b4e457b455a9d8961ee12c5938213131eb3834596385a5d0d81cfea5ebff2
MD5 6961b033ab95d28f929898fe8ee67545
BLAKE2b-256 4cd108d151b5eee152e386f0dd9a90f03b2f5cebe8442efe8eec8e8b1cdd28eb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: libgossip-0.2.1-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 207.4 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.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 bcf4d499b1fc3bee9cb710283d3fd44fc44161f5c2dbcecdcbe63389f6765129
MD5 775979aaefe597e67ae4d851aaf0f721
BLAKE2b-256 d2a0811461bd91d956235ebcfa2c864a17d03da7b7686f10846668b425270d7b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libgossip-0.2.1-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 5176995a7fdcd953ccf6ab3417b522a0f36bbe8695d7f259a020a655d8c80f67
MD5 f0c6c776665d3c44fb499d5066763a9a
BLAKE2b-256 e8affeb8b1a8f4d964ae996cf63c5f66af2bc8366e53c8493c7f93522028b101

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libgossip-0.2.1-cp311-cp311-musllinux_1_1_i686.whl
Algorithm Hash digest
SHA256 7bca39e147a8010543501d595d81f91aebdb065e57b419a2ee8f9dc82124d4a6
MD5 0efba5d0a4f6ac9fd9a57033ff82e292
BLAKE2b-256 ae37e07390bc94f1e4b576678bff834f28cd516e731808c680ca3eb8bcbb544b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libgossip-0.2.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 029842213cd5e4ca49ec9a45b8d6d9b345e99fb5b1b7de46f7d5e963dd63154b
MD5 35cb8575aa46cd11dc03479b5333e9a8
BLAKE2b-256 4bb144e7c5a88eaf05315333cb5039912318fe9487babd808151e718c0c01e72

See more details on using hashes here.

File details

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

File metadata

  • Download URL: libgossip-0.2.1-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 206.8 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.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 1ef453f206afccf7d30ed049a8565516fe296501df7b5c121570d327adbfb51c
MD5 4bc9223a477b1a18a4c64ef10e0696bf
BLAKE2b-256 60238693145662e8c98e995290fb33d2a86b3234fc324774d54d3f72a0e47423

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libgossip-0.2.1-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 e85eed68a3cad82d11b11384c329792e5214602220b969d0ff241353d93f6ccb
MD5 69fbac2c709a434011113b4cc3848de1
BLAKE2b-256 f721c32b1e805efb351815c8ece04438734b8beb4de3ca85853695ee10e9688a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libgossip-0.2.1-cp310-cp310-musllinux_1_1_i686.whl
Algorithm Hash digest
SHA256 aaf23f5ba5a11158507fe965f26afdfe7cd6a8cb3603325014789294f647ceb1
MD5 93c7c1fbd37584953de4eee582edb51d
BLAKE2b-256 96d9401a347c80d888527ab247ed2b625f5a81ca487a40cf2faa103bc41d14f5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libgossip-0.2.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 28351a6c30e2e22c441e9c6d8d887c5b9ced841f4c1e726e9a1ac797ee889448
MD5 d672315a6731436d4195bfc9c2b969ff
BLAKE2b-256 3bef1e61b18cad229433a77ce93830e0be8bfd1b4f101f36bd2b08ca2af8ced2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: libgossip-0.2.1-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 216.4 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.1-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 2b702edd5385093684dd62c3a4c90aecc2ec84fb0aeba94cca7b20bd25e5da48
MD5 f233c41231527535e2a354668dc49157
BLAKE2b-256 704957e1f8a0dce3975ddb036c6f23f4550ada4cb566215e689be4b8392de087

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libgossip-0.2.1-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 2e933442cf0fcf8e15ea8e91a653511e30568316ba684df55c9acacc246ab5ce
MD5 674f259bc5938793fb2e0b8978ac3dae
BLAKE2b-256 5dbf5053e343d7e782e8a4a2072a7f5aabfc4c8866d11c8591c1cc73b4e182ec

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libgossip-0.2.1-cp39-cp39-musllinux_1_1_i686.whl
Algorithm Hash digest
SHA256 1eb8898143808fd68ec5d4ebb5ce0befe49a5272e2845481d0c3d028dcfaee1a
MD5 c4cebc0ddc4f7b5665600be7d5213b48
BLAKE2b-256 e5ea74cd61eb997c3ff9e2c61c10081020268c4467c588fefa4f74d4f93dd20e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libgossip-0.2.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6a265b287ed37d6305342318203cddc6c31ba8be01d5d38fd837b59bc09c4575
MD5 1bc8b79fe00b9c1ec0a26e473faa7574
BLAKE2b-256 b34f0dcb9c0abfc5d1be6ce299dd05242f38bd90dbe5f3ec6754aeff4d1c8e69

See more details on using hashes here.

File details

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

File metadata

  • Download URL: libgossip-0.2.1-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 206.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.1-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 79c7f9b039cd8a9c0dcc7590b638b5d29d83fb996958361e8eac5f408ff758fc
MD5 96dc31ee37348ba6fc8588aafcdc494e
BLAKE2b-256 fd67e66a2d6327697f7b5eabf3fe4f1967f5eed21b3797c672f8371f7f2b938a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libgossip-0.2.1-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 d0e14d5b2e903e649214336703fa44f2555fdb593cf65adbf6439ef8651cef70
MD5 a0d08eecafe600ec09b695d3251b26ac
BLAKE2b-256 a5c6f8aad004b20adb1649c818d9468ebebf6cb12a0714ecd90d592bb55c6f79

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libgossip-0.2.1-cp38-cp38-musllinux_1_1_i686.whl
Algorithm Hash digest
SHA256 f7f9b52fa3ca97e0c902a493d0b19eee29693a6991a6c16dfbcddb87c6c472ff
MD5 8ab5c9afd9b320db5438d8bc05dadc3d
BLAKE2b-256 ba6dcb58bc70b59a0477ad013d14d3a6253963b494129174cd9ba0af9480cd58

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libgossip-0.2.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3a6f01ec9f20f39bf84fb6f57f265fd6b3edab19e64864b45ed1b82cbb5488e9
MD5 10a80d1d0884df88f41105c11b7653fc
BLAKE2b-256 f190eabd4d0a6d0d0823ad18cbbf8fd4ab110789e4a80d525a13caea454b5555

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