Distributed¶
casty.Distributed
¶
Facade for creating distributed data structures.
Spawns sharded actor regions lazily, per structure name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gateway
|
EntityGateway
|
Backend that provides access to sharded entities. Both
|
required |
journal
|
EventJournal | None
|
If provided, structures use event sourcing for durability. |
None
|
Examples:
>>> d = Distributed(system)
>>> counter = d.counter("hits", shards=50)
>>> users = d.map[str, User]("users", shards=10)
>>> tags = d.set[str]("tags")
>>> jobs = d.queue[Task]("jobs")
map
property
¶
set
property
¶
queue
property
¶
__init__(gateway, *, journal=None)
¶
counter(name, *, shards=100, timeout=5.0)
¶
Create a distributed counter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Logical counter name (used as entity ID). |
required |
shards
|
int
|
Number of shards for the backing region. |
100
|
timeout
|
float
|
Default timeout for counter operations. |
5.0
|
Returns:
| Type | Description |
|---|---|
Counter
|
|
Examples:
lock(name, *, shards=100, timeout=5.0)
¶
Create a distributed lock.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Logical lock name (used as entity ID). |
required |
shards
|
int
|
Number of shards for the backing region. |
100
|
timeout
|
float
|
Default timeout for lock operations. |
5.0
|
Returns:
| Type | Description |
|---|---|
Lock
|
|
Examples:
semaphore(name, permits, *, shards=100, timeout=5.0)
¶
Create a distributed semaphore with permits concurrent holders.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Logical semaphore name (used as entity ID). |
required |
permits
|
int
|
Maximum number of concurrent holders. |
required |
shards
|
int
|
Number of shards for the backing region. |
100
|
timeout
|
float
|
Default timeout for semaphore operations. |
5.0
|
Returns:
| Type | Description |
|---|---|
Semaphore
|
|
Examples:
barrier(name, *, node_id=None, shards=10, timeout=60.0)
¶
Create a distributed barrier.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Logical barrier name (used as entity ID). |
required |
node_id
|
str | None
|
Identifier for this node (defaults to |
None
|
shards
|
int
|
Number of shards for the backing region. |
10
|
timeout
|
float
|
Default timeout for the barrier wait. |
60.0
|
Returns:
| Type | Description |
|---|---|
Barrier
|
|
Examples:
casty.EntityGateway
¶
Bases: Protocol
Protocol for backends that provide access to sharded entities.
Both ClusteredActorSystem and ClusterClient satisfy this
protocol structurally, allowing Distributed to work with either.
casty.Barrier
¶
Client for a distributed barrier backed by a sharded actor.
Multiple nodes call arrive(expected) and all block until expected
nodes have arrived.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
system
|
ActorSystem
|
The actor system for sending messages. |
required |
region_ref
|
ActorRef[ShardEnvelope[BarrierMsg]]
|
Reference to the shard proxy / region. |
required |
name
|
str
|
Barrier name (used as entity ID). |
required |
node_id
|
str
|
Identifier for this node (typically |
required |
timeout
|
float
|
Default timeout for the barrier wait. |
60.0
|
Examples:
casty.Counter
¶
Client for a distributed counter backed by a sharded actor.
Provides increment, decrement, and get operations that
route through the shard proxy to the correct entity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
system
|
ActorSystem
|
The actor system for sending messages. |
required |
region_ref
|
ActorRef[ShardEnvelope[CounterMsg]]
|
Reference to the shard proxy / region. |
required |
name
|
str
|
Counter name (used as entity ID). |
required |
timeout
|
float
|
Default timeout for each operation. |
5.0
|
Examples:
casty.Dict
¶
Client for a distributed key-value map backed by sharded actors.
Each key maps to a separate entity actor (entity-per-key pattern).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
system
|
ActorSystem
|
The actor system for sending messages. |
required |
region_ref
|
ActorRef[ShardEnvelope[MapEntryMsg]]
|
Reference to the shard proxy / region. |
required |
name
|
str
|
Map name prefix (combined with key for entity ID). |
required |
timeout
|
float
|
Default timeout for each operation. |
5.0
|
Examples:
>>> users = d.map[str, dict]("users")
>>> await users.put("alice", {"age": 30})
>>> await users.get("alice")
{'age': 30}
casty.Lock
¶
Client for a distributed mutual-exclusion lock.
Each Lock instance has a unique owner ID. acquire blocks until
the lock is granted; try_acquire returns immediately.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
system
|
ActorSystem
|
The actor system for sending messages. |
required |
region_ref
|
ActorRef[ShardEnvelope[LockMsg]]
|
Reference to the shard proxy / region. |
required |
name
|
str
|
Lock name (used as entity ID). |
required |
timeout
|
float
|
Default timeout for each operation. |
5.0
|
Examples:
casty.Queue
¶
Client for a distributed FIFO queue backed by a sharded actor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
system
|
ActorSystem
|
The actor system for sending messages. |
required |
region_ref
|
ActorRef[ShardEnvelope[QueueMsg]]
|
Reference to the shard proxy / region. |
required |
name
|
str
|
Queue name (used as entity ID). |
required |
timeout
|
float
|
Default timeout for each operation. |
5.0
|
Examples:
casty.Semaphore
¶
Client for a distributed counting semaphore.
Each Semaphore instance has a unique owner ID. acquire blocks
until a permit is available; try_acquire returns immediately.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
system
|
ActorSystem
|
The actor system for sending messages. |
required |
region_ref
|
ActorRef[ShardEnvelope[SemaphoreMsg]]
|
Reference to the shard proxy / region. |
required |
name
|
str
|
Semaphore name (used as entity ID). |
required |
timeout
|
float
|
Default timeout for each operation. |
5.0
|
Examples:
casty.Set
¶
Client for a distributed set backed by a sharded actor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
system
|
ActorSystem
|
The actor system for sending messages. |
required |
region_ref
|
ActorRef[ShardEnvelope[SetMsg]]
|
Reference to the shard proxy / region. |
required |
name
|
str
|
Set name (used as entity ID). |
required |
timeout
|
float
|
Default timeout for each operation. |
5.0
|
Examples:
>>> tags = d.set[str]("tags")
>>> await tags.add("python")
True
>>> await tags.contains("python")
True