https://github.com/fancycode/python-continuum
Consistent hashing for Python.
https://github.com/fancycode/python-continuum
Last synced: 8 months ago
JSON representation
Consistent hashing for Python.
- Host: GitHub
- URL: https://github.com/fancycode/python-continuum
- Owner: fancycode
- License: lgpl-2.1
- Created: 2010-11-14T23:33:19.000Z (about 15 years ago)
- Default Branch: master
- Last Pushed: 2014-09-21T20:19:06.000Z (over 11 years ago)
- Last Synced: 2025-05-07T12:13:06.197Z (8 months ago)
- Language: Python
- Homepage: http://www.joachim-bauch.de/projects/python-continuum/
- Size: 132 KB
- Stars: 11
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.txt
- License: LICENSE.txt
Awesome Lists containing this project
README
python-continuum - Consistent hashing
========================================================================
Copyright (c) 2010 by Joachim Bauch, mail@joachim-bauch.de
http://www.joachim-bauch.de/projects/python-continuum/
python-continuum provides a function to do consistent hashing. See
wikipedia [1]_ for more informations about this technique.
First we need the continuum object that serves as storage for server
informations and can later be used to resolve keys::
>>> from continuum import Continuum
>>> c = Continuum()
Empty continuums obviously can't be queried::
>>> c.resolve('my-key1')
Traceback (most recent call last):
...
IndexError: empty continuum
Add the server objects that are available as backends::
>>> c.add_server('192.168.0.1', 8080)
>>> c.add_server('192.168.0.2', 8080)
You can also specify different capacities of the server to priorize
them (this defaults to 1)::
>>> c.add_server('192.168.0.3', 8080, 2)
Server objects can also be removed::
>>> server = c.add_server('192.168.0.4', 8080)
>>> c.remove_server(server)
>>> len(c)
3
Please note that a server can only be added once::
>>> c.add_server('192.168.0.2', 8080)
Traceback (most recent call last):
...
TypeError: server already added
After all servers have been added, the continuum can be queried for the
server that should be used for a given (string) key::
>>> c.resolve('my-first-key')
>>> c.resolve('my-other-key')
.. [1] http://en.wikipedia.org/wiki/Consistent_hashing