larzactor
Actor-model concurrency without locks. Pure Python, zero dependencies.
An actor owns its state and a mailbox; it processes one message at a time in its own thread, so its state is never touched by two threads at once - no locks, no data races, by construction. You talk to actors only by sending messages.
from larzactor import Actor, ActorSystem
class Counter(Actor):
def __init__(self):
self.count = 0
def receive(self, message):
if message == "inc":
self.count += 1
elif message == "get":
return self.count
system = ActorSystem()
ref = system.spawn(Counter)
for _ in range(1000):
ref.tell("inc")
ref.ask("get") # 1000 - exact, even under concurrent senders
system.shutdown()
Why
- Concurrency you can reason about. State lives inside an actor and is only ever touched by that actor's thread, so you write ordinary sequential code with no locks - and get correct results even when many threads send at once (the tests hammer one actor from 4 threads and count is exact).
- tell and ask.
tellis fire-and-forget;asksends and waits for the handler's return value (re-raising any exception it threw). - Supervision. A handler that raises doesn't kill the actor - a supervisor
on_errorhook is notified and the actor keeps processing. - Zero dependencies. Plain threads + queues.
Install
pip install larzactor
Usage
from larzactor import Actor, ActorSystem
system = ActorSystem(on_error=lambda ref, msg, exc: log(exc))
ref = system.spawn(MyActor, *init_args) # Actor subclass
ref = system.spawn(lambda msg: msg * 2) # or a plain callable
ref.tell(message) # fire-and-forget
result = ref.ask(query, timeout=5) # request/response
ref.stop()
system.shutdown()
Tests
python -m unittest discover -s tests -v # 8 tests incl. a no-lost-updates race test
The Larz stack
One of 30+ pure-Python, zero-dependency libraries at github.com/larz-scripter.
License
MIT (c) larz-scripter
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file larzactor-0.1.0.tar.gz.
File metadata
- Download URL: larzactor-0.1.0.tar.gz
- Upload date:
- Size: 5.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9f39b8e02983eb455b81d1b2717160cdd2277968358fe03f894ad10f124c866a
|
|
| MD5 |
e472b28427fc587e03f02406484c99ee
|
|
| BLAKE2b-256 |
b9fe0f776f913aa21e978646e41c3678d9fe9650a3fad62f73974c2cd9977c18
|
File details
Details for the file larzactor-0.1.0-py3-none-any.whl.
File metadata
- Download URL: larzactor-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
feebf14edfa72038645fb349984e68de13bda4dbabf52ec40e2002a9af9d3768
|
|
| MD5 |
f0417b77efac88138d0a956df09055e4
|
|
| BLAKE2b-256 |
2d0d23fa05f4dec894fd134d3901686651f3231b83c9f2bfe091c38586e609c5
|