root / examples / pingpong / scala.py

1
#!/usr/bin/env python
2
3
# cf. Scala by Example, Chapter 3
4
5
import time
6
import random
7
import inspect
8
import sys
9
import os.path
10
11
sys.path[0:0] = [ os.path.join( os.path.dirname( inspect.getabsfile( inspect.currentframe() ) ), '..', '..', 'lib' ) ]
12
13
from logging import warning
14
15
import dramatis
16
17
class Ping (dramatis.Actor):
18
19
    def __init__(self,times,pong):
20
        self.pings_left = times
21
        dramatis.release( pong ).ping( self )
22
23
    def pong(self,caller):
24
        if self.pings_left % 1000 == 0:
25
            print "Ping: pong"
26
        if self.pings_left > 0:
27
            self.pings_left -= 1
28
            dramatis.release( caller ).ping( self )
29
            
30
class Pong (dramatis.Actor):
31
32
    def __init__(self):
33
        self.pong_count = 0
34
35
    def ping(self,caller):
36
        if self.pong_count % 1000 == 0:
37
            print "Pong: ping", self.pong_count
38
        self.pong_count += 1
39
        dramatis.release( caller ).pong( self )
40
41
pong = Pong()
42
ping = Ping( 1000, pong )