Skip to main content

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', 'google', 'tumblr', 'yahoo', '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

Release files for splut 18

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for splut 18
File Size Uploaded
splut-18.tar.gz 7.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for splut 18
File Interpreter ABI Platform
splut-18-py3-none-any.whl Python 3 none any Details

Total release size: 15.3 kB

Release files / splut-18.tar.gz

Download URL splut-18.tar.gz
Size 7.5 kB
Tags Source
SHA-256 checksum
How to use checksums
d6e48697ba4763c8acfd35bdf522fb725031af0d91e27bc6fe1b23a917b8e22f
BLAKE2b-256 checksum
How to use checksums
f29df04b9852ed8f4459c00ab374c9e444ebc8032da6b2a98d49bf411347068e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.12

Release files / splut-18-py3-none-any.whl

Download URL splut-18-py3-none-any.whl
Size 7.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c67d3291c1211837445babe67439573c87010b09f79f362c73c7ff138be522bb
BLAKE2b-256 checksum
How to use checksums
63c981ea428fd3a8baaaae09bf8871c1067353a2eb031047777bac31bc92a97f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.12

Release history Release notifications | RSS feed

30

2 release files

28

2 release files

27

2 release files

26

2 release files

25

2 release files

24

2 release files

23

2 release files

22

2 release files

21

2 release files

20

2 release files

19

2 release files

This release

18 This release

2 release files

17

2 release files

16

2 release files

15

2 release files

14

2 release files

13

2 release files

12

2 release files

11

2 release files

10

2 release files

8

2 release files

7

2 release files

6

2 release files

5

2 release files

4

2 release files

3

2 release files

2

2 release files

1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page