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

Uploaded CPython 3.12Windows x86-64

libgossip-0.3.0-cp312-cp312-musllinux_1_1_x86_64.whl (940.9 kB view details)

Uploaded CPython 3.12musllinux: musl 1.1+ x86-64

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

Uploaded CPython 3.12musllinux: musl 1.1+ i686

libgossip-0.3.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (425.3 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

libgossip-0.3.0-cp311-cp311-win_amd64.whl (246.8 kB view details)

Uploaded CPython 3.11Windows x86-64

libgossip-0.3.0-cp311-cp311-musllinux_1_1_x86_64.whl (940.2 kB view details)

Uploaded CPython 3.11musllinux: musl 1.1+ x86-64

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

Uploaded CPython 3.11musllinux: musl 1.1+ i686

libgossip-0.3.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (425.3 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

libgossip-0.3.0-cp310-cp310-win_amd64.whl (246.3 kB view details)

Uploaded CPython 3.10Windows x86-64

libgossip-0.3.0-cp310-cp310-musllinux_1_1_x86_64.whl (938.9 kB view details)

Uploaded CPython 3.10musllinux: musl 1.1+ x86-64

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

Uploaded CPython 3.10musllinux: musl 1.1+ i686

libgossip-0.3.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (424.7 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

libgossip-0.3.0-cp39-cp39-win_amd64.whl (256.5 kB view details)

Uploaded CPython 3.9Windows x86-64

libgossip-0.3.0-cp39-cp39-musllinux_1_1_x86_64.whl (939.5 kB view details)

Uploaded CPython 3.9musllinux: musl 1.1+ x86-64

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

Uploaded CPython 3.9musllinux: musl 1.1+ i686

libgossip-0.3.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (424.7 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

libgossip-0.3.0-cp38-cp38-win_amd64.whl (246.2 kB view details)

Uploaded CPython 3.8Windows x86-64

libgossip-0.3.0-cp38-cp38-musllinux_1_1_x86_64.whl (939.1 kB view details)

Uploaded CPython 3.8musllinux: musl 1.1+ x86-64

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

Uploaded CPython 3.8musllinux: musl 1.1+ i686

libgossip-0.3.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (424.2 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

File details

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

File metadata

  • Download URL: libgossip-0.3.0.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.3.0.tar.gz
Algorithm Hash digest
SHA256 c2d8c6fb3008f8c5e5548a672390871ec306374a080d6508b50662692c4c4bbb
MD5 28c245c877ac869c4c9467761fea9546
BLAKE2b-256 a1a1bcdfb118a6cc5fdb481332c685eb828817652af3db87aa2904972193a758

See more details on using hashes here.

File details

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

File metadata

  • Download URL: libgossip-0.3.0-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 248.7 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.3.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 a5f7f6bd2b1029a576d8f06402d5d7c84ec88f007fbf4aa5846af6453c7c7f4f
MD5 35d52a1ca46fa7ffc6cde8122f0a5bb8
BLAKE2b-256 600f0db0019fea28f36be5ecc0285057ada30c6df3c88e7ffe221bbd1889173e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libgossip-0.3.0-cp312-cp312-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 237fe56354ff3a0ebf9efb64668d1b1797a1b015b4491d551da5d7ea8b3b74be
MD5 0914557482321d39ff2ba480cd65a26b
BLAKE2b-256 f551a496c041777a89a3c13f36031a8e02f850ef867ebecbd7934f3cc724e247

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libgossip-0.3.0-cp312-cp312-musllinux_1_1_i686.whl
Algorithm Hash digest
SHA256 e49c907f3c143219aa6c0eeadd67f9e27229367e11547e72231a5e4e38025f55
MD5 eaf5cf5ee6d16e49ec14a772f3d37997
BLAKE2b-256 f4a93e36d772eb5d5b8f42621cb58f9ad9dea4bf643ac5fdca2a6b98d43fa66f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libgossip-0.3.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f0d74816ec1383f45f872a4f7280b25fdb973f6a966af04e97645a3dbfb4bd27
MD5 db21c0f9cf4b2e5e6ea80dc59778347a
BLAKE2b-256 6764bef49a6f291f2aec6aa7fd5f1ff6bd2ad7dce43241e92c1c8a17325e605e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: libgossip-0.3.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 246.8 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.3.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 6ee5e2fa812f1f7e991f09daaa1056151d1bd59130b29b06a820ce6864e5ed70
MD5 1b7a2b36d6f41915120d916129383840
BLAKE2b-256 7fb38f2a71e2b4c42cd3b57b2f36e0171ea9552bb58d6de6a8719c1d8cdde434

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libgossip-0.3.0-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 60b3d652b30851db7b4dfe03c6f4c3167296f500957ec21999e3d09098065e8b
MD5 b7d30033797f6d8d0bc4ce362189c806
BLAKE2b-256 8ca3ae344f30956cc37a212cafd56dc43a2834995b06b0d7f6c0cb1b7b4c27f4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libgossip-0.3.0-cp311-cp311-musllinux_1_1_i686.whl
Algorithm Hash digest
SHA256 bcf5ca0b088b96e1a6d85e46151708bc0bb32538e1f95faaed2370fa38460bb0
MD5 97411b34b6590fe3191c943642a8c634
BLAKE2b-256 2f7cfd8a6abc64bb321d34f5a0b064e1851d51442a8c86049b5b10ae11944f61

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libgossip-0.3.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1613c322a697c7f37be12058c7f860b5208d99bfd69f18d20fb37920a4623a37
MD5 eec6eb0a2c6d00691e8b7c191b9ba2f5
BLAKE2b-256 85528bf9339a1d6cd81c8d92935ea1e8cac54407c9219c8a609b4d434e828b6a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: libgossip-0.3.0-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 246.3 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.3.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 6cb12bfb61d2a1f765c3cb92d8cf64a2e60f354df733477eb3ca4493cf976f46
MD5 ade921ca3c5aade54282715e9ae4adb7
BLAKE2b-256 053458ade04b83d7b0c76f2cce125f0fa7ef198bbd7f39c015cf034c6e18c3ee

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libgossip-0.3.0-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 38754e94134352ee74c2d81195803a76e544ec604598f3b84cdb167b035a39a5
MD5 f7d1fec72be3d6131585b816e51127fa
BLAKE2b-256 22a7347ad6394a4d69bc0f53f5eb6a9c02f0e1e940d9b1f99dcaeaa751e28edd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libgossip-0.3.0-cp310-cp310-musllinux_1_1_i686.whl
Algorithm Hash digest
SHA256 09ff4dad435cad445a35e0550d0573f154f38f7af918e129c9720275003e384b
MD5 88302138231fb5786f6e5ffd50c6f29b
BLAKE2b-256 b9610b54b0d463919e1d16ad2e4aba10d8d2eaadf0f743f6f513eed695ecb372

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libgossip-0.3.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 81754cfa54d6bc3f51d9fd11e1a2368716ec3107905ce79ab9ff9372369fa229
MD5 a514d5ef2e793e4889611350ae80e0aa
BLAKE2b-256 098d0898aad3ba7afbe2a3db63b8a1eee28f8447091787e9775760478c58d547

See more details on using hashes here.

File details

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

File metadata

  • Download URL: libgossip-0.3.0-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 256.5 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.3.0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 86fac6de2e1265692393b3abfa465e4b84e4b801c8e5b8b28ab2add3cf75eab5
MD5 2090631b3244bea8723e98214f989729
BLAKE2b-256 4b4669dd038e0c0cebda068cd906b1e9443b2d335e16efdc679cc61621aad1bf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libgossip-0.3.0-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 100d180be8da420d5a45cd185f454b65bdbeac985a917a45704c1c44a802456b
MD5 3a6efef7e120b5f6553723c3fc20874a
BLAKE2b-256 1fcbc92a863bdc8a72e7e45cae1da8fbe74df0ad914962a648de36a8107e3b9b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libgossip-0.3.0-cp39-cp39-musllinux_1_1_i686.whl
Algorithm Hash digest
SHA256 03921a4607c918123986da5b967382142b0ba5520738bcb24deaaa0fad654e25
MD5 2902600f9470abe64c0d51a08007fc73
BLAKE2b-256 8c6705a674ddf24a8be5786ab5632e807de28a7f06204d0d931e5f59fa578853

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libgossip-0.3.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f70cd5b9def66e3de25d97a05ade2749e7ea61fb44b32025cf12addba51ba81d
MD5 4462c6e8e9ddcb195f58282b7939ed5e
BLAKE2b-256 43291177471230f48a45501e3a97e6e071fb34618b2e3eebce59032309087069

See more details on using hashes here.

File details

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

File metadata

  • Download URL: libgossip-0.3.0-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 246.2 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.3.0-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 1ba0b4e77736cef5ea1bcf38593b54047fd33584c2b85f9edad26ad724b61309
MD5 10b67c71b2dcf39581c266c4b2d7bb94
BLAKE2b-256 a421af5fa187f3cc42ea241838843170ed3fa7843c9ccaaef0669e2e05c94bc3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libgossip-0.3.0-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 a3d2ac90f7e008771da5c7421c731ee27f9248bfc8748f6c7613e0d343dde70b
MD5 c9012329ae56664209f1ad505ea8dfba
BLAKE2b-256 1da6762b63a18c9db9f0aa30094f17468045dc5be5151bb091afba5557a3e912

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libgossip-0.3.0-cp38-cp38-musllinux_1_1_i686.whl
Algorithm Hash digest
SHA256 e0367bffbb91e06e6b34d8b01f7e8553c55a0504c71f4befd9ef1637b467905e
MD5 0762ccfaabf21f224a2030eb303cf406
BLAKE2b-256 8993e1c03941a319238dc2106e86c44c266cfe0aa292bb02d5c210a35e4fb692

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libgossip-0.3.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3526f9067640e3e8f6fd6be2c4c054c475c917c562944006897082f7f4c4ed44
MD5 8e48beea55d9b77fa300a66840eb6ce1
BLAKE2b-256 858dcc11dbd812a00a06df9f74d7d649496f536db6ba956c8cc3c882a0be8960

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