Skip to main content

A minimalist actor framework in Python.

Project description

A minimalist actor framework in Python.

  • Free software: MIT license

Installation

pip install pactor

Running the Example

Clone the repo locally, then run the following commands:

pipenv install
pipenv run python -m pactor

To exit, press q.

Documentation

The actor model is a computational model that is useful for concurrent execution. See: https://en.wikipedia.org/wiki/Actor_model.

pactor is a minimalist implementation of the actor model in Python, using multiprocessing.

The actor model stipulates that actors only interact with each other through messaging. pactor implements that restriction by building a proxy around an actor class and converting method calls into messages.

To wrap a pickleable class as an Actor, simply create an Actor with an instance of the target class, as follows:

class MyActor:
    def __init__(self, name):
        self.name = name

    def some_method(self):
        ...

actor_instance = Actor(MyActor())
The Actor class provides a couple of key capabilities:
.proxy
A proxy object that has methods that mirror those on the wrapped class. Calling a method on the proxy will generate a message to the actor process with the provided parameters.

.close()
Signals that the actor process should discontinue processing messages and terminate.

.join()
Blocks the calling thread until the actor process terminates.

Additionally, the actor class itself is enhanced with an enqueue method that can be used to send messages to itself.

Consider this simple example of a Monitor:

class Monitor:
    def __init__(self, name, aggregator):
        self.name = name
        self.aggregator = aggregator
        self.status = 0

    def read_status(self):
        self.status = fetch_status()
        self.aggregator.update_status(self.name, self.status)
        self.enqueue(self.read_status) # queue up another read

And an Aggregator:

class Aggregator:
    def update_status(self, target_name, status):
        print('Status update for %s: %s' % (target_name, status))

These could be used as follows:

def main():
    aggregator = Actor(Aggregator('aggregator'))
    primary_mon = Actor(Monitor('primary', aggregator.proxy))
    secondary_mon = Actor(Monitor('secondary', aggregator.proxy))

    primary_mon.read_status()
    secondary_mon.read_status()

    aggregator.join()

This simple example highlights several critical points:

  • Each Actor class will actually run in a separate process

  • One Actor can be passed to another Actor using the .proxy member

  • Invoking a method on an Actor proxy does not directly invoke that method on the calling thread, but instead is wrapped as a message and passed to the actor process.

Development

To run the all tests run:

tox

Changelog

0.0.1 (2019-12-10)

  • First release on PyPI.

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

pactor-0.1.0.tar.gz (15.7 kB view details)

Uploaded Source

Built Distribution

pactor-0.1.0-py2.py3-none-any.whl (7.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pactor-0.1.0.tar.gz.

File metadata

  • Download URL: pactor-0.1.0.tar.gz
  • Upload date:
  • Size: 15.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.4

File hashes

Hashes for pactor-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a3a9619a1e4916c2b9c0980481573f6284f8a856c340b65dbe7849c71b9172ed
MD5 35cd11bfa986da330b09465b529e0438
BLAKE2b-256 0e0614be9e063657c53beaed10d77270579b8052fe5be33feb06c3dfa6cf34a4

See more details on using hashes here.

File details

Details for the file pactor-0.1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: pactor-0.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.4

File hashes

Hashes for pactor-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 97db34f4cb564aa2fb20d32b5c9c124da6d4f007f1a87a4fe73f86de741ae53e
MD5 42d0a3df913ff39e0e2a31233411c966
BLAKE2b-256 681f474d22afa0319eaa250d30c68a4d0fc783aa4839f94bf65d4d3d9c6974bb

See more details on using hashes here.

Supported by

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