# Collection of code snippets by Arne Vajhøj 
# (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
import threading
import time
from pykka import *


TEST1_DONE = '__DONE__'

TEST2_DONE = '__DONE__'

class Test2(ThreadingActor):
    def on_receive(self, msg):
        if msg == TEST2_DONE:
            self.stop()
        else:
            print('test2: %s (thread: %d)' % (msg, threading.get_ident()))
            return 'ACK'

class Test1(ThreadingActor):
    def __init__(self):
        super().__init__()
        self.test2 = Test2.start()
    def on_receive(self, msg):
        if msg == TEST1_DONE:
            self.test2.tell(TEST2_DONE)
            self.stop()
        else:
            print('test1: %s (thread: %d)' % (msg, threading.get_ident()))
            replymsg = self.test2.ask(msg)
            print('reply received: %s (thread: %d)' % (replymsg, threading.get_ident()))

print('main (thread: %d)' % (threading.get_ident()))
test1 = Test1.start()
test1.tell('Hello wold!')
test1.tell('Hello wold!')
test1.tell('Hello wold!')
time.sleep(0.1)
test1.tell(TEST1_DONE)
    
