-
-
Notifications
You must be signed in to change notification settings - Fork 106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How do pykka actors send a message to themselves? #24
Comments
Something like this should work: import time
import pykka
class StringPrinter(pykka.ThreadingActor):
def on_receive(self, message):
print 'I got a message:', message
class StringProducer(pykka.ThreadingActor):
def __init__(self, target, string):
super(StringProducer, self).__init__()
self.target = target
self.string = string
self.running = False
def on_receive(self, message):
if message.get('command') == 'start':
self.running = True
self.produce()
elif message.get('command') == 'stop':
self.running = False
elif message.get('command') == 'produce':
self.produce()
def produce(self):
if not self.running:
return
time.sleep(1)
self.target.tell({
'command': 'string_delivery',
'string': self.string,
})
self.actor_ref.tell({'command': 'produce'})
consumer = StringPrinter.start()
producer = StringProducer.start(consumer, 'apple')
producer.tell({'command': 'start'})
time.sleep(3)
producer.tell({'command': 'stop'})
producer.stop()
consumer.stop() The trick is to use |
Awesome. In reality the inelegance of Hmmm. This question is outside the scope of the issue, but can you recommend any HTTP libraries that perform requests asynchronously that would work well with this pykka approach? (quite a useful piece of information for other developers too I'm sure) |
I haven't used much HTTP libs, so I don't off hand know of any async HTTP libs for Python. Ignoring async/sync, I'd have a look at |
Well do with this issue as you will. I'm happy for it to be closed, though you might want to attach any changes relevant to it before you do so. |
So, I am trying to do something similar using the It seems to me that if you take the asynchronous nature of actors seriously, the implementation is allowed to take as long as it likes to deliver a message. To guarantee timeliness, the example would have to be re-written so that the producer runs in an actual loop rather than tail-recursing through actor-messages, but that puts us back at the suggestion from the initial poster. |
I'm writing a simple
Producer
actor that's supposed to spit out a string every second. At creation time, it doesn't do anything.When I send it a 'start' message it should
start
producing the string I nominate to the target I nominate.When I send it a 'stop' message it should
stop
producing.If we were doing this like Erlang, there'd be a sort of tail-recursive call that serves as the continuation of the process. But that's all a bit deep for Python. All we have is the
on_receive
function, which may or may not block.My first stab looks something like this:
I don't even bother looking at the command type (
start
orstop
) because I feel stuck already. Is this the way to do it?I would start a producer and give it a poke as to begin its production loop:
How would you guys do it?
The text was updated successfully, but these errors were encountered: