Skip to main content

Actor model for Python

Project description

splut

Actor model for Python

Usage example

>>> from concurrent.futures import ThreadPoolExecutor
>>> from splut.actor import Join, Spawn
>>> from urllib.request import urlopen
>>> import sys
>>>
>>> class Pipe:
...
...     traffic = 0
...
...     def __init__(self, index):
...         self.index = index
...
...     def fetch(self, url):
...         def log(message):
...             print(f"[{self.index}] [{url}] {message}", file = sys.stderr) # Demonstrate worker (and thus thread) utilisation.
...         log('Fetch.')
...         with urlopen(url) as f:
...             data = f.read()
...         n = len(data)
...         log(f"Got: {n}")
...         self.traffic += n # Effectively self is locked, so this is thread-safe.
...         return data
...
>>> class Sum:
...
...     def __init__(self, pipeactor):
...         self.pipeactor = pipeactor
...
...     async def bytecount(self, dotcoms):
...         futures = [self.pipeactor.fetch(f"https://www.{dotcom}.com/") for dotcom in dotcoms] # Collect futures eagerly.
...         return sum(map(len, await Join(futures)))
...
>>> pipes = [Pipe(i) for i in range(3)]
>>> with ThreadPoolExecutor() as e:
...     spawn = Spawn(e)
...     pipeactor = spawn(*pipes) # One mailbox, multiple independent workers.
...     sumactor = spawn(Sum(pipeactor)) # An actor is cheap to create, unlike a thread.
...     bytecount = sumactor.bytecount(['facebook', 'github', 'google', 'tumblr', 'youtube']).wait()

Total bytes piped by workers matches the total we have:
>>> sum(p.traffic for p in pipes) == bytecount
True

API

splut.actor

Spawn Objects

class Spawn()

__init__
def __init__(executor)

Spawned actors will use threads from the given executor.

__call__
def __call__(*objs)

Create an actor backed by the given worker object(s), each of which is used in a single-threaded way. Calling a method on the returned actor returns a Future immediately, which eventually becomes done with the result of a worker method of the same name (or never if the worker method hangs). A worker method may be async, in which case it can await futures returned by other actors, releasing the worker in the meantime.

Join Objects

class Join()

Make multiple futures awaitable as a unit. In the zero futures case this resolves (to an empty list) without suspending execution. Otherwise if any future hangs, so does this. Otherwise if any future failed, all such exceptions are raised as a chain. Otherwise all results are returned as a list.

splut.actor.future

Future Objects

class Future()

wait
def wait()

Block until there is an outcome, then return/raise it. For use outside actors, or within one if you know the future is done and don't want to suspend execution with await in that case.

andforget
def andforget(log)

Send any exception to the given log.

splut.bg

Sleeper Objects

class Sleeper()

interrupt
def interrupt()

If a sleep is in progress that sleep returns now, otherwise the next sleep will return immediately. This is similar behaviour to interrupting a maybe-sleeping thread in Java.

splut.bg.delay

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

splut-19.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

splut-19-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file splut-19.tar.gz.

File metadata

  • Download URL: splut-19.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for splut-19.tar.gz
Algorithm Hash digest
SHA256 d687226b94baa05864508b4ddb230aca01bfb15f3c09fb008c6b328dc3dca1fe
MD5 232259abd2f48e665861e4829298e050
BLAKE2b-256 72b22f75bd9c3c606ea92b52cb436dd9b9ef678e9cddb29d0a76b45eed1122ff

See more details on using hashes here.

File details

Details for the file splut-19-py3-none-any.whl.

File metadata

  • Download URL: splut-19-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for splut-19-py3-none-any.whl
Algorithm Hash digest
SHA256 8dc8c9107a7ba7395589d5da4bb8fa7b8dec4874719850c8c794c2050e399cd6
MD5 08c5bc892c08146a4d1a8fcc81ddbf7f
BLAKE2b-256 f7c90e05fd455023f5f6100987d9e9b0b99dd0ec0f1e323526c8f81167ada7f3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page