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
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.
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()
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?