Skip to main content

Distributed leader election and locking using MongoDB

Project description

MongoElector

Distributed leader election and locking using MongoDB.

Components

  • MongoLocker - Standalone distributed lock primitive backed by MongoDB with TTL-based expiration
  • MongoLeaderElector - Leader election coordinator built on MongoLocker with background polling and callbacks

Installation

pip install mongoelector

Quick Start

Distributed Lock

from pymongo import MongoClient
from mongoelector import MongoLocker

db = MongoClient().my_database
lock = MongoLocker("my-resource", db, ttl=30)

lock.acquire()
try:
    # do work while holding the lock
    pass
finally:
    lock.release()

Leader Election

from pymongo import MongoClient
from mongoelector import MongoLeaderElector

db = MongoClient().my_database

def became_leader():
    print("I am the leader now")

def lost_leadership():
    print("No longer the leader")

elector = MongoLeaderElector(
    "my-service",
    db,
    ttl=15,
    on_leader=became_leader,
    on_leader_loss=lost_leadership,
)
elector.start()

Requirements

  • Python >= 3.10
  • pymongo >= 4.13

License

Apache 2.0

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

mongoelector-1.0.1.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

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

mongoelector-1.0.1-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file mongoelector-1.0.1.tar.gz.

File metadata

  • Download URL: mongoelector-1.0.1.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mongoelector-1.0.1.tar.gz
Algorithm Hash digest
SHA256 eac58440a7be10647b7abb96f428dcba289bc21d11f107973da2f8daf87df10b
MD5 f1bbe9647e92e6209db79e06a85c6a3f
BLAKE2b-256 b03966a9a0eaa65acbbf509f4adb5e9788891f82cab0dc9a08ddec68337d26ac

See more details on using hashes here.

Provenance

The following attestation bundles were made for mongoelector-1.0.1.tar.gz:

Publisher: workflow.yml on zebpalmer/MongoElector

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mongoelector-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: mongoelector-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mongoelector-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 040d4aa7d2fe33fa437e7dc77ba79ea88a7077e7ea2e2d4a16327c20a97bf799
MD5 7bf4c7a336d0bdd795ad611f35f3e5fb
BLAKE2b-256 8f7bdb60d5291fb1822a3e4f8bc5ee93c1df19c6535555537ea96f2572228eb3

See more details on using hashes here.

Provenance

The following attestation bundles were made for mongoelector-1.0.1-py3-none-any.whl:

Publisher: workflow.yml on zebpalmer/MongoElector

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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