I tried to start the loop in the receiveMessage_Run() method (see code below), but I cannot to end the loop. The actor is blocked to receive new messages.
Can somebody help me with this task? How can I run infinite loop (it seems, asynchronous) and save possibility to react to other messages and possibility to stop the loop and actor?
My testing code:
from thespian.actors import ActorExitRequest, ActorSystem, Actor, ActorTypeDispatcher
import time
import random
# Message 'Run'
class Run():
pass
# Message 'Stop'
class Stop():
pass
class Actor(ActorTypeDispatcher):
def __init__(self):
self.stop = False
# It seems, it must be asynchronous
def loop(self):
while not self.stop:
time.sleep(1)
print(random.random())
def receiveMsg_Run(self, data, sender):
self.loop()
self.send(sender, 'Finished the loop.')
def receiveMsg_Stop(self, msg, sender):
self.stop = True
self.send(self.myAddress, ActorExitRequest)
print('*' * 1000)
def receiveMsg_ActorExitRequest(self, msg, sender):
self.stop = True
time.sleep(1)
if __name__ == '__main__':
asys = ActorSystem('multiprocQueueBase')
loop = asys.createActor(Actor)
resp = asys.ask(loop, Run())
print(resp)
time.sleep(4)
asys.tell(loop, Stop())
ActorSystem().shutdown()
I tried to start the loop in the receiveMessage_Run() method (see code below), but I cannot to end the loop. The actor is blocked to receive new messages.
Can somebody help me with this task? How can I run infinite loop (it seems, asynchronous) and save possibility to react to other messages and possibility to stop the loop and actor?
My testing code: