Async model actor library
Project description
astage
Initial MVP for Async actor model library for Python
Currently zero external dependencies outside of the Python standard library.
Features:
- Concurrent actor model with asyncio
- Maps message types to handler methods on the actor
- Tell and ask methods for sending messages to the actor
- Allows setting backpressure on the actor mailbox
Example
import asyncio
from dataclasses import dataclass
from astage import Actor, handler
@dataclass
class IncrementMessage:
value: int
@dataclass
class EchoMessage:
text: str
class CounterActor(Actor):
def __init__(self):
super().__init__()
self.count = 0
@handler
async def increment(self, message: IncrementMessage):
self.count += message.value
print(f"Count is now: {self.count}")
@handler
async def echo(self, message: EchoMessage):
return f"Echo: {message.text}"
async def main():
actor = CounterActor()
# start the actor which will run in a non-blocking asyncio.Task
handle = await actor.start()
# tell: send a message without waiting for a response
await handle.tell(IncrementMessage(5))
# Expected: Count is now: 5
await handle.tell(IncrementMessage(5))
# Expected: Count is now: 10
# ask: send a message and wait for a response
result = await handle.ask(EchoMessage("Hello, Actor!"))
print(result) # Expected: Echo: Hello, Actor!
# it is possible to use tell and ask on all @handler methods
# the return value of the ask will be the return value of the handler
response = await handle.ask(IncrementMessage(5))
print(response) # Expected: None
if __name__ == "__main__":
asyncio.run(main())
The message types also works with Pydantic classes, although Pydantic is not a dependency of the library.
import asyncio
from pydantic import BaseModel
from astage import Actor, handler
class IncrementMessage(BaseModel):
value: int
class CounterActor(Actor):
def __init__(self):
super().__init__()
self.count = 0
@handler
async def increment(self, message: IncrementMessage):
self.count += message.value
return f"Count is now {self.count}"
async def main():
actor = CounterActor()
handle = await actor.start()
# create a message using a Pydantic class
pydantic_msg = IncrementMessage(value=10)
# ask: send a message and await a response
result = await handle.ask(pydantic_msg)
print(result) # Expected: Count is now 10
if __name__ == "__main__":
asyncio.run(main())
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
astage-0.1.0.tar.gz
(12.2 kB
view details)
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 astage-0.1.0.tar.gz.
File metadata
- Download URL: astage-0.1.0.tar.gz
- Upload date:
- Size: 12.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.5.29
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b6b38a9995de8a8a436f558d0411de0eef47dec32ad796687f2be152ef31b5b8
|
|
| MD5 |
03c9d65487de683ffac3a9e199bed2de
|
|
| BLAKE2b-256 |
e37a5c4a52aead95197c3a95739b802807c6deaf737fb788b08188cc3c5529d0
|
File details
Details for the file astage-0.1.0-py3-none-any.whl.
File metadata
- Download URL: astage-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.5.29
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cfa048ade189e04790958cf3d117ab6864d12a500e7f1f525b04a61e0fcdcb7a
|
|
| MD5 |
e442d55ef61155aa02509eb27eaa9f96
|
|
| BLAKE2b-256 |
3b73b8406b4ae5ccb339697da2318b0305acc2d7ea195fe45d621450e224405a
|