I've learned. I'll share.

June 27, 2008

Message Passing Conccurrency (Actor Model) in Python

As I wrote previously, I'm a fan of message-passing concurrency. But to use it in python, I had to write my own infrastructure. It's far from perfect, but it's working for me. I've simplified it a bit and prepared some code to share with you.

The basic idea of my framework is to be and simple and easy to use as possible. There's no Erlang-style pattern matching or process linking. There isn't even any built-in error handling. But, there's a lot of support for the typical "react loop" style of message handling.

The architecture is very simple. There's one thread per actor. An actor is just an object you can send a message to. The actor receives the messages in the order they were sent. You always know the sender of the message; it's built right in. There's also "Bridge" that allows you to interact with actors from non-actor code.

The only tricky thing you'll see is a "handles" decorator. This is used to help you identify how you want an actor to handle a given message type. It helps you avoid re-creating a message loop and dispatcher yourself for every actor.

I've included a little example of how to use the framework. It obviously quite simple, but keep in mind that I used little more for the foundation of a rather complex file synchronization application. A little bit goes a long way.

So, here it is. Cut, paste, and run.

from threading import Thread, Event
from Queue     import Queue, Empty

class IActor:
    # send(message, sender)

class HandlerRegistry(dict):
    # should be used as a decorator
    def __call__(self, typ):
        def register(func):
            self[typ] = func
            return func
        return register

OtherMessage = "Other Message"

class Stop(Exception):
    def __repr__(self):
        return "Stop()"

class Stopped:
    def __repr__(self):
        return "Stopped()"

class ActorNotStartedError(Exception):
    def __init__(self):
        Exception.__init__(self, "actor not started")

class Actor(IActor):
    def spawn(cls, *args, **kargs):
        self = cls(*args, **kargs)
        self.mailbox = Mailbox()
        start_thread(target = self.act, as_daemon = True)
        return self

    def send(self, message, sender):
        if self.mailbox is None:
            raise ActorNotStartedError()
            self.mailbox.send(message, sender)

    def receive(self):
        if self.mailbox is None:
            raise ActorNotStartedError()
            return self.mailbox.receive()

    # override if necessary
    def act(self):

    handles = HandlerRegistry()

    def makeHandles(*classes):
        return HandlerRegistry((typ, handler) for cls in classes for (typ, handler) in cls.handles.iteritems())

    def handleMessages(self):
            while True:
                message, sender = self.receive()
                self.handleMessageWithRegistry(message, sender)
        except Stop:

    def handleMessageWithRegistry(self, message, sender):
        registry = self.__class__.handles
        handler  = registry.get(message.__class__) or registry.get(OtherMessage)
        if handler is not None:
            handler(self, message, sender)

    def onOther(self, message, sender):

    def onStop(self, message, sender):
        sender.send(Stopped(), self)
        raise message

def start_thread(target, as_daemon, name = None):
    thread = Thread(target = target)
    if name:
    return thread

class Mailbox:
    def __init__(self):
        self.mailbox = Queue()

    def send(self, message, sender):
        self.mailbox.put((message, sender), block = False)

    def receive(self, timeout = None):
        return self.mailbox.get(block = True, timeout = timeout)

class Bridge(IActor):
    def __init__(self):
        self.mailbox = Mailbox()

    def send(self, message, sender):
        self.mailbox.send(message, sender)

    def call(self, target, request, timeout, default = None):
        self.sendRequest(target, request)
        return self.receiveResponse(timeout, default)

    # targeted_requests can be an iterator
    def multiCall(self, targeted_requests, timeout, default = None):
        count = 0
        for target, request in targeted_requests:
            self.sendRequest(target, request)
            count += 1
        for _ in xrange(count):
            yield self.receiveResponse(timeout, default)

    def stop(self, actors, timeout):
        stop = Stop()
        return list(self.multiCall(((actor, stop) for actor in actors), timeout, default = None))

    def sendRequest(self, target, request):
        target.send(request, self)

    def receiveResponse(self, timeout, default):
            message, sender = self.mailbox.receive(timeout = timeout)
            return message
        except Empty:
            return default

if __name__ == "__main__":
    import time

    class GetInventory:

    class Task:
        def __init__(self, input, destination):
            self.input       = input
            self.destination = destination

    class Worker(Actor):
        handles = Actor.makeHandles()

        def __init__(self, skill):
            self.skill = skill

        def onTask(self, task, sender):
            output = self.skill(task.input)
            task.destination.send(output, self)

    class Warehouse(Actor):
        handles = Actor.makeHandles()

        def __init__(self):
            self.inventory = []

        def onGetInventory(self, message, sender):
            # copy the inventory to avoid anyone mutating it
            sender.send(list(self.inventory), self)
        def onTaskResult(self, result, sender):

    worker    = Worker.spawn(lambda x : x * 2)
    positives = Warehouse.spawn()
    negatives = Warehouse.spawn()
    bridge    = Bridge()

    for val in [1, 2, 3, -2, -4, -6]:
        warehouse = positives if val >= 0 else negatives
        worker.send(Task(val, warehouse), sender = None)

    print bridge.call(positives, GetInventory(), 1.0) #should be [ 2,  4,   6]
    print bridge.call(negatives, GetInventory(), 1.0) #should be [-4, -8, -12]
    print bridge.stop([worker, positives, negatives], 1.0) #should be [Stopped(), Stopped(), Stopped()]

    class Start:
        def __init__(self, target):
            self.target = target

    class Ping:
        def __repr__(self):
            return "Ping()"

    class Pong:
        def __repr__(self):
            return "Pong()"

    class Pinger(Actor):
        handles = Actor.makeHandles()

        def onStart(self, start, sender):
            start.target.send(Ping(), self)

        def onPong(self, pong, sender):
            print "-",
            sender.send(Ping(), self)
    class Ponger(Actor):
        handles = Actor.makeHandles()

        def onPing(self, ping, sender):
            print "+",
            sender.send(Pong(), self)

    # should print lots of +-+-+-
    pinger = Pinger.spawn()
    ponger = Ponger.spawn()
    pinger.send(Start(ponger), sender = None)
    bridge.stop([pinger, ponger], 1.0)


  1. did you took at look at

  2. Very nice and small and clean.

    A couple of thoughts:

    In many ways, Erlang's selective receive is one of the most critical parts of Erlang. Without it, application code gets pretty complex, trying to manage when messages can be handled and when they need to be deferred. It's a key part of blocking RPC-like functions, which are, in turn, a key part of Erlang's gen_servers.

    One other comment: Python threads are pretty expensive. Having one per actor means the number of actors you can have is pretty limited (which might be fine for you need; and this true even if they get garbage-collected unless you pool them.) In actually, actors don't need a thread per actor, just a thread per message concurrently delivered, which can be a lot less. Of course, it's a lot messier too, though.

  3. I did look at Twisted. It promises all of the things I need for distributed programming, but it didn't deliver (for me). Anytime you pull a 3rd party dependency into your code, you have to weight the cost against the benefit. With Twisted, the cost is pretty big because it's a big, complex thing and you can't really work with part of it without using all of it. The benefit wasn't there for me either, since many of their design choices didn't match my needs.

  4. Selective receive is very nice, especially when mixed with power pattern matching.
    I started to add it, but it got complex pretty quickly, and I thought "am I ever going to really need this?". So I left it out. There have been a few times when I wish I had it, but on the whole, I haven't needed it.

    The big difficulty is that to really get the power of selective receive you need to have pattern matching, but I haven't yet come up with an elegant way to do pattern matching in python.
    This is one of the ways that Erlang is way beyond other programming languages.

  5. Yes, threads are expensive. One thread per actor doesn't scale very much. So, I've also created and experimental threadless actor library. I'll post about it soon.

    I'd like to try and combine threadless and threaded actors so that you have exactly as many threads as you need. The Scala guys wrote a good paper about it a few years ago, but no one seems to have done much with it yet.

  6. And what about stackless ? http://www.stackless.com/

  7. I copied the selective semantics that a couple of other Ruby Actor packages have: match the message against an array of objects: if the object is not a class, do an == match. If the object is a class, do an instanceof match. The message itself can have more elements than the pattern, which are ignored.

    Not the same as Erlang, which can, for example, allow you to require two elements of a message be the same, but so far seems to be working for folks. I figure at some point, we'll need a catch-all case a bit more like Erlang guards for situations where that kind of matching doesn't work.

    I've been looking at thread allocations for actors. Right now, I have two conflicting requirements that I'm trying to balance. On the one hand, one reason I want actors to execute in separate threads is that I want them to be able to block on I/O without blocking the rest of the world. Given that I'm not willing to restrict what an actor method can do, this pretty much requires giving them their own thread, at least for the duration of that call.

    On the flip side, not restricting the number of threads allows things to go bad quickly in some cases. Doug Lea did a paper on needing to restrict threads in Java for embarrassingly parallel things like naive fib. So far I haven't seen that problem in real code, so it's not on the top of my list (though it will probably fall out of some other work to make dramatis work with GUI libraries which require single-threaded access.)

    I'll be interested to see how your threadless stuff works and combines with threaded.

  8. The Scala guys have some really good papers about combining threadless and threaded actors: http://lamp.epfl.ch/~phaller/doc/haller07coord.pdf and http://lampwww.epfl.ch/~odersky/papers/jmlc06.pdf. There approach is the one I'm going to take.

    Basically, you have threadless actors that "schedule" their actions rather than executing them directly. Then, you have a "controller" (threaded) actor that hands those actions out to worker (threaded) actors. If any worker actor blocks for more than a specific amount of time, you spawn another worker actor. This way, you only spawn as many threaded actors as necessary, but many blocking IO calls can continue to run in parallel.

  9. Your pattern matching technique sounds a lot like candygram: http://candygram.sourceforge.net/. Have you seen it? I think it's pretty good and probably as good as you can get in Python.

  10. Yes, I have seen stackless. If they are successful, it would make Python MUCH better for doing things like continuations, monads, message-passing, etc. I'm very much hoping for their success. But it's still too immature to rely on for
    the application I'm writing.

  11. The blog has a to an extraordinary degree restrain focus that opens up, shockingly into broad assortment of concerns.

  12. Love to read it,Waiting For More new Update and I Already Read your Recent Post its Great Thanks. celebrities verified


  13. Thanks for sharing this informative content.,
    Leanpitch provides online training in Scrum Master Certification during this lockdown period everyone can use it wisely.
    Join Leanpitch 2 Days CSM Certification Workshop in different cities.
    CSM online training

    CSM training online

  14. I love this. It is soo informative. Are you also searching for swedish assignment writers we are the best solution for you. We are best known for delivering the best services to students without having to break the bank

  15. Now it very well may be used for intermittent family unit use to standard every day printer in the office.if you need all data

  16. Excellent content ,Thanks for sharing this .,
    Leanpitch provides online training in CSPO, everyone can use it wisely.

    Product owner certification
    Product owner training

  17. This content helped me alot you can check my article here. Flutter App development company

  18. In my opinion you shoud check the
    class Stopped:
    def __repr__(self):
    return "Stopped()"

    class ActorNotStartedError(Exception):
    def __init__(self):
    Exception.__init__(self, "actor not started")

    there is something missing in the operators

  19. Additional thick for warmth, yet breathable for use while working out, our men's coats and covers are made with the best cotton wool material.

  20. Such a great blog .I am very happy to read this blog .This blog is really informative and knowledgeable for your readers .Thanks for sharing this blog and good information.
    Now it's time to avail town car service Houston for more information.

  21. تعتبر اسعار ماكينات تعبئة وتغليف البهارات ليست بالثمينه مقارنةً بباقي اسعار ماكينات التعبئة والتغليف، حيث يمكنك اقامة مشروع تعبئة وغتغليف متكامل بسبب هذه الماكينة فتقوم بتعبئة مختلف انواع البودر مثل البهارات والتوابل والدقيق والملح والخ الخ، كما تقدم ماسترتك ضمان صيانة دورية للماكينة لتحافظ الماكينة علي ادائها و جودتها العالية.

  22. A study conducted by the University of Florida in 2005 found that more than 50% of college students had experienced some form of stress-related injury, which included ankle sprains. According to the American Academy of Orthopedic Surgeons, between 20 and 30 percent of people who have ankle sprains suffer from chronic ankle instability (CAI), which is defined as recurring ankle sprains and pain.

  23. The procedure will go smoothly from beginning to end becauseCEMENT TREATED BASE services in USA of our skilled team of contractors' attention to detail. Because we are aware of how crucial projects with a short turnaround time

  24. This competition aims to promote brands404 esports in Dubai while fast raising brand recognition.

  25. Due to TACB's considerable experience with a range Offshore credit limits in Dubaiof financial instruments, the sale will be completed without a hitch.

  26. , and chemical injection solutions tochemical injection pumps in Dubai the international oil and gas industry

  27. . Through our business focused approach, we PPC marketing dubaithrive to deliver state of the art

  28. We're confident that you'll find the ideal piece of chocolatebest chocolate in saudi to suit every mood with such a huge variety to choose from.

  29. If you're looking for a reliable and trustworthy service to "take my online exam", MyAssignmentHelp.sg is the perfect choice. The tutors are committed to delivering top-quality work and ensuring that you receive the best possible grades.

  30. This is such a great article. I am very glad to read this article, because it contains many useful information which helps us a lot. The article is very interesting for the readers. Keep sharing with us the more article. Now its time to avail Limo service West Palm Beach for more information.

  31. Python is such a highly programming used in future. Such an interesting and useful article is. It contains many important points. Thanks for sharing with us. Now its time to avail Limo service West Palm Beach for more information.

  32. I am lookig for that type of python script thanks for sharing. I truly value this post because it's such a well-done piece. You offer information that is helpful. It really gave me some new information, and I do agree that reading blogs can help us learn more about a variety of subjects by allowing us to obtain solid information. I really appreciate this wonderful post that you have provided for us. I assure you this would be beneficial for most people. Now its time to avail party bus dc for more information.

  33. You are doing really good work. Python is very difficult language but you explained very well. I am impressed from your this work. Now its time to avail Sign and Blind Services in Eastbourne for more information.

  34. Your blogs are really good and interesting. It is very great and informative. You always know the sender of the message; it's built right in. There's also "Bridge" that allows you to interact with actors from non-actor code bankruptcy lawyers virginia beach. I got a lots of useful information in your blog. Keeps sharing more useful blogs..

  35. Python programming is one of the latest programming. This is an admirable Python model that you mention in this blog post which is very informative. Now it's time to avail Jupiter Florida Airport for more information.

  36. New York Divorce Waiting Period
    The game's gameplay is exhilarating, offering an immersive experience for both newcomers and seasoned gamers. Its intuitive controls make it easy to engage, and its depth of strategy provides endless creative problem-solving opportunities. The pacing is well-balanced, keeping excitement high without overwhelming players. The game's dedication to delivering a top-notch gaming experience makes it a must-play for fans of the genre.

  37. The author regrets not having specific information or reviews for "valuedlessons" or any related entity up to September 2021. They suggest using search engines, social media platforms, or online communities and forums to find up-to-date information and user experiences about this relatively new or niche entity. They suggest using these resources to access relevant discussions and reviews related to "valuedlessons."abogado flsa en virginia

  38. The speaker acknowledges that they lack access to real-time information about "valuedlessons reviews" and recent reviews, as their knowledge only extends to September 2021. They encourage asking specific questions or requesting general answers based on their current knowledge, but acknowledge that they cannot access the internet to access recent reviews or information.Abogado FLSA

  39. The article "Message Passing Concurrency (Actor Model) in Python" offers a comprehensive introduction to the Actor Model, a crucial concept in concurrent programming. It breaks down key components and demonstrates their implementation in Python, providing practical examples and case studies. The article also offers suggestions for further reading and resources, making it an educational and well-written resource for those interested in concurrent programming in Python.
    Leyes Matrimoniales de Nueva York Divorcio


Blog Archive

Google Analytics