A Quick Look at the Pykka Code Library

Pykka is a Python language code library (or framework depending on your point of view) that helps you write programs that use the Actor paradigm. This is useful when you have many components that need to interact with each other at the same time — sometimes called concurrency.

The basic idea of the Actor programming paradigm is that you create Actor objects that communicate with each other using only messages. Each Actor object has its own state and cannot directly affect the state of other Actor objects. An example of a scenario where the Actor paradigm would be useful is in a game with different players doing things at different times.

You could implement an Actor-based system from scratch, but Actor systems are very complex. There are a stunning number of Actor frameworks that give you a head start. One problem with these Actor frameworks is that there are complex frameworks that can handle complex problem scenarios but they require a huge effort just to get a Hello World example running; there are simple Actor frameworks that allow you to get some examples running relatively quickly but they don’t have enough power to handle complex problem scenarios.

An Actor framework that is medium complexity is the Pykka framework. I spent a couple of hours looking at the Pykka documentation and coded up an example. The key code is:

actor = SimpleActor.start()
actor.tell({'x' : 3.1})
actor.tell({'y' : 4.2})

msg_lst = actor.ask("get_messages", timeout=3)
print("\nCurrent messages: ")
print(msg_lst)

res = actor.ask("sum", block=False)
ans = res.get()
print("\nSum of values: %0.1f " % ans)

actor.stop()

This code creates a Pykka Actor object using start(), accepts two messages that feed the Actor some data using tell(), prints the saved data as a response to an ask(), then computes a result using ask().

Even this simple Actor example is extraordinarily complicated and there’s a lot of mystery behind the scenes. And that’s the point of this blog post. When you are creating a complex system with interacting components, there are no simple solutions. But for such complex problem scenarios, the Actor paradigm is a good approach in my opinion.

Actor-based programming is similar to agent-based programming. In my opinion the differences between the two paradigms are mostly a matter of semantics.


My three favorite pairs of actors who’ve played Sherlock Holmes and Dr. Watson. Left: Basil Rathbone and Nigel Bruce appeared in 14 films from 1939 to 1946. Center: Robert Downey Jr. and Jude Law appeared in two films (2009 and 2011). Right: Arthur Wontner and Ian Fleming (not the Bond author) appeared in five films from 1931 to 1937.

This entry was posted in Miscellaneous. Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s