jodal / pykka

🌀 Pykka makes it easier to build concurrent Python applications.
https://pykka.readthedocs.io
Apache License 2.0
1.22k stars 107 forks source link

Implementation of ActorSystem, Dispatchers and Executors #34

Open chris-zen opened 10 years ago

chris-zen commented 10 years ago

Hi, recently I have been learning about Scala and Akka and I became amazed by such technologies. The problem is that I have some ongoing projects that are already binded to Python language. I was looking for similar things in Python and found Pykka. It looks great to me, as I don't need all the features that Akka have, so I am considering seriously to start using it. Congratulations for the current work !

However there is something that I don't understand from the current design. Why actors are binded to a given threading technology and can not be decided on runtime, for example from the user configuration ? When you define class MyActor(ThreadingActor) there is no way to use this same actor logic with gevent without touching the source code (let's assume that gevent monkey patch doesn't exist). Or imagine you want to expand pykka to use asyncio and want to start using it in current projects...

From what I have been seeing in the source code it does not look so difficult to solve this problem, but it would require to change a bit the overall design of Pykka. I have already seen the issue #18 and I completely agree with it. This would allow not only having independent actor systems but also having an inbox, future and event loop factory per system, which is the only thing that {Threading,Gevent,Eventlet}Actor's are doing (please correct me if I am wrong).

It would be called ActorSystem and have an actor factory method actorOf (just like in Akka) that would create actor instances with a reference to the actor system that created it. The ActorSystem would implement the methods _create_inbox, _create_future and _create_event_loop that would delegate to the selected threading technology implementation.

ActorSystem would have a constructor that would allow to configure it. As a replacement for Akka Props it could be used the builtin library functools.partial.

Just as a reference:

import pykka
from functools import partial

class Greeter(pykka.Actor):
    def __init__(self, greeting='Hi there!'):
        super(Greeter, self).__init__()
        self.greeting = greeting

    def on_receive(self, message):
        print(self.greeting)

system = pykka.ActorSystem(using=pykka.THREADS)
actor_ref = system.actorOf(partial(Greeter, greeting='Hi you!'))

What do you think ? If we agree on a design I could make a fork and start working on it if anyone else is interested in doing it.

jodal commented 10 years ago

Hi Christian! Thanks for you well researched ideas and good example.

I'm open to changing the model from subclassing specific actor types to having executors live a bit more on the side of the actors themselves. That could also allow for actor systems that have N actors sharing a pool of M executors, where N > M.

I'll keep this issue open and I'll refer to it when I get around to sitting down and working on the next minor or major version of Pykka.

chris-zen commented 10 years ago

Hi @jodal, I see that this is something you have been already thinking about. I have been looking in deep at Akka source code and documentation and there are some important abstractions that could be incorporated to pykka:

These would be the basic and most important things to work on in pykka. But just to not forget about, there are two other things that could also be incorporated:

If I get some time (...) I will start to work a bit on these things on my fork. Let's see.

drozzy commented 10 years ago

This would indeed be the most-greatest-awesome addition. I would also move the scheduler up there into the important list.