Simple proxy example (help)

60 views
Skip to first unread message

馬杰鴻

unread,
May 24, 2015, 3:50:32 PM5/24/15
to python...@googlegroups.com
Hello!

I wish to try pulsar to use the actors model in my project (thus "removing" a singleton).

I had a look at Pykka, which does a subset of what pulsar can do, if I understand correctly. One of them, is an "actor proxy". And even if Pulsar seems to offer this, I do not understand how to implement it correctly.

Here is a simple example with Pykka:

import pykka

class Data(pykka.ThreadingActor):
    pykka_traversable
= True
   
def __init__(self):
       
super(Data, self).__init__()
       
self.saved_value = 0
   
def inner_method(self):
       
return 42

if __name__ == '__main__':
    actor_ref
= Data.start()
    actor_proxy
= actor_ref.proxy()
   
print(actor_proxy.inner_method().get())
    actor_proxy
.saved_value = 10
   
print(actor_proxy.saved_value)

# Output:
# 42
# 10


In this code, the Data class has 1 attribute, and 1 method.
Thanks to the proxy, we can directly call the method of assign a value to the attribute.
In the background, messages are passed to the actor to actually satisfy this behaviour.

lsbardel first told me about the Greeter example in Pulsar, but it's not the same.

How is this kind of behaviour achievable with Pulsar?

Thanks ;)

Jiehong

lsbardel

unread,
May 25, 2015, 2:27:54 PM5/25/15
to python...@googlegroups.com, ma.ji...@gmail.com
Hi Jiehong,


On Sunday, May 24, 2015 at 8:50:32 PM UTC+1, 馬杰鴻 wrote:
Hello!

I wish to try pulsar to use the actors model in my project (thus "removing" a singleton).

Removing which singleton?
 

I had a look at Pykka, which does a subset of what pulsar can do, if I understand correctly. One of them, is an "actor proxy". And even if Pulsar seems to offer this, I do not understand how to implement it correctly.

I don't know, Pykka, but for what I see pulsar Actor API is different in two respects:

1) Pulsar is asynchronous therefore any message passing between actors needs to take that into account.
2) Pulsar does not subclass the Actor class to add behaviour, instead it uses functions performing a task and send them to the target actor

Your application can be rewritten as

from pulsar import arbiter, spawn, send, async, Config


def start(arbiter, **kw):
    async(app(arbiter))


def app(arbiter):
    # Spawn a new actor
    proxy = yield from spawn(name='actor1')
    print(proxy.name)
    # Execute inner method in actor1
    result = yield from send(proxy, 'run', inner_method)
    print(result)

    yield from send(proxy, 'run', set_value, 10)
    value = yield from send(proxy, 'run', get_value)
    print(value)

    # Stop the application
    arbiter.stop()


def inner_method(actor):
    return 42


def set_value(actor, value):
    actor.saved_value = value


def get_value(actor):
    return actor.saved_value


if __name__ == '__main__':
    cfg = Config()
    cfg.parse_command_line()
    arbiter(cfg=cfg, start=start).start()



lsbardel

unread,
May 25, 2015, 2:34:20 PM5/25/15
to python...@googlegroups.com, luca.sb...@gmail.com, ma.ji...@gmail.com
I have included the example in the snippets module in examples:


馬杰鴻

unread,
May 25, 2015, 2:56:29 PM5/25/15
to python...@googlegroups.com, ma.ji...@gmail.com
Thank you for your answer.

Therefore, if I understand correctly, there is not such a thing as "Active object" in Pulsar, while it is the case with Pykka.

Now I understand that a function must take an actor as an argument in order to be run within an actor.

I have a question though: what is the set of all known functions to a given actor?

In the example you kindly provided, I may guess that any spawned actor is aware of any function define in the current module, is that correct?
If so, is it possible to make an actor being able to run functions from a given module only?

lsbardel

unread,
May 25, 2015, 3:58:56 PM5/25/15
to python...@googlegroups.com, ma.ji...@gmail.com


On Monday, May 25, 2015 at 7:56:29 PM UTC+1, 馬杰鴻 wrote:
Thank you for your answer.

Therefore, if I understand correctly, there is not such a thing as "Active object" in Pulsar, while it is the case with Pykka.

No, each Actor is active in its own domain, at the same time.
 

Now I understand that a function must take an actor as an argument in order to be run within an actor.

Yes, the first argument of the function sent to an actor with the run command must accept the actor as first argument (http://quantmind.github.io/pulsar/design.html#run)


I have a question though: what is the set of all known functions to a given actor?

In the example you kindly provided, I may guess that any spawned actor is aware of any function define in the current module, is that correct?

Not really.
 
If so, is it possible to make an actor being able to run functions from a given module only?

The actor will run any callable you send to it via the "run" message.
It is up to you where you define the callables, they can be defined anywhere, they can be even an object with the __call__ method, the only requirements is this callable are picklable.


馬杰鴻

unread,
May 25, 2015, 4:17:13 PM5/25/15
to python...@googlegroups.com, ma.ji...@gmail.com
Well, thank you very much! Things are much clearer now!
Reply all
Reply to author
Forward
0 new messages