# 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_ACK = '__ACK__'
TEST1_DONE = '__DONE__'

TEST2_DONE = '__DONE__'

class Test2(ThreadingActor):
    def __init__(self, test1):
        super().__init__()
        self.test1 = test1
    def on_receive(self, msg):
        if msg == TEST2_DONE:
            self.stop()
        else:
            print('test2: %s (thread: %d)' % (msg, threading.get_ident()))
            self.test1.tell(TEST1_ACK)

class Test1(ThreadingActor):
    def __init__(self):
        super().__init__()
        self.test2 = Test2.start(test1=self.actor_ref)
    def on_receive(self, msg):
        if msg == TEST1_ACK:
            print('test1: ACK from test2 (thread: %d)' % (threading.get_ident())) 
        elif msg == TEST1_DONE:
            self.test2.tell(TEST2_DONE)
            self.stop()
        else:
            print('test1: %s (thread: %d)' % (msg, threading.get_ident()))
            self.test2.tell(msg)

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)
    
