Quick start
Install#
pip install clusterio # or: uv add clusterioclusterio needs Python 3.10 or later and runs on Linux and macOS.
One node#
With nothing configured, clusterio is a one-node cluster that keeps its data in .clusterio/local. Save this as counter.py:
import os
import time
import clusterio
@clusterio.transaction
def bump(name):
counts = clusterio.dict("counts")
counts[name] = counts.get(name, 0) + 1
return counts[name]
def main(rounds=3):
node = os.environ.get("CLUSTERIO_NODE", "local")
for _ in range(rounds):
try:
print(node, "counts visit", bump("home"))
except clusterio.Unavailable:
print(node, "waits for a majority of the nodes")
time.sleep(1)
if __name__ == "__main__":
main()$ python counter.py
local counts visit 1
local counts visit 2
local counts visit 3
$ python counter.py
local counts visit 4
...The count survives because the node's log and state are on disk. bump is a transaction: it reads and writes as one step, so two processes bumping at once never lose a visit.
Three nodes#
Now run the same file as three nodes. Each node needs its name and the address of every node, itself included. Open three terminals:
$ export CLUSTERIO_PEERS=a=127.0.0.1:7001,b=127.0.0.1:7002,c=127.0.0.1:7003
$ CLUSTERIO_NODE=a clusterio run counter.py:main # terminal 1
$ CLUSTERIO_NODE=b clusterio run counter.py:main # terminal 2
$ CLUSTERIO_NODE=c clusterio run counter.py:main # terminal 3clusterio run starts the node, calls main(), and stops the node when main returns. Until a second node is up, a has no majority and waits. Then the three nodes count one counter between them:
a counts visit 1
b counts visit 2
c counts visit 3
a counts visit 4
...On real hosts, put each host's address in CLUSTERIO_PEERS instead of 127.0.0.1. The code did not change: where the cluster is lives in the environment, not in the program. See Configuration and CLI.
Three nodes in one process#
For tests and experiments, make the nodes yourself with Cluster:
import tempfile
import time
from clusterio import Cluster
peers = {"a": "127.0.0.1:17101", "b": "127.0.0.1:17102", "c": "127.0.0.1:17103"}
with tempfile.TemporaryDirectory() as data:
nodes = [Cluster(node=n, peers=peers, data=f"{data}/{n}") for n in peers]
try:
nodes[0].dict("greetings")["en"] = "hello"
while "en" not in nodes[2].dict("greetings"):
time.sleep(0.01) # a read is local: c sees the write once it is replicated
print(nodes[2].dict("greetings")["en"])
finally:
for node in nodes:
node.close()Next#
- Shared data: what a
dict, alistand a file can hold. - Transactions: the tool that makes coordination easy.
- Examples: complete applications.