Skip to main content

Pykka is easy to use concurrency using the actor model

Project description

Pykka is a concurrency abstraction which makes actors look like regular objects.

It lets you call methods on an actor like you would on a regular object, but it runs the code in the actor’s own thread. Similarily, when you access the actor’s fields, they are read in the actor’s thread, serialized and copied to the reading thread.

Both method calling and attribute reads returns futures which ensures that the calling thread does not block before the value from the object’s thread is actually used.

Pykka is similar to typed actors in the Akka framework.

What can it do?

Given the following code:

from pykka import Actor

class Adder(Actor):
    def add_one(self, i):
        print '%s: %d' % (self.name, i)
        return i + 1

class Counter(Actor):
    def count_to(self, target):
        i = 0
        while i < target:
            print '%s: %d' % (self.name, i)
            i = self.other.add_one(i + 1).get()

if __name__ == '__main__':
    adder = Adder().start()
    counter = Counter(other=adder).start()
    counter.count_to(10).get() # Block until finished
    counter.stop()
    adder.stop()

We get the following output:

$ PYTHONPATH=. python examples/counter.py
Thread-2: 0
Thread-1: 1
Thread-2: 2
Thread-1: 3
Thread-2: 4
Thread-1: 5
Thread-2: 6
Thread-1: 7
Thread-2: 8
Thread-1: 9

See the examples/ dir for more runnable examples.

License

Pykka is licensed under the Apache License, Version 2.0. See LICENSE for the full license text.

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

Pykka-0.1.tar.gz (6.9 kB view details)

Uploaded Source

File details

Details for the file Pykka-0.1.tar.gz.

File metadata

  • Download URL: Pykka-0.1.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for Pykka-0.1.tar.gz
Algorithm Hash digest
SHA256 31a5d9537f1978b7e244924f877c75b9ec3ebf3f95527eb10dbcb2ff2348e9ae
MD5 e860a70ec902ab1ab7c41f4bb4aaf1fc
BLAKE2b-256 7682c1aae56257a9a29deb70a99e78e61e9850e062569076d2f30ed34149ebd0

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