root / examples / pingpong / actor.py

1
#!/usr/bin/env python
2
3
# cf. Scala by Example, Chapter 3
4
5
import inspect
6
import sys
7
import os.path
8
import time
9
10
sys.path[0:0] = [ os.path.join( os.path.dirname( inspect.getabsfile( inspect.currentframe() ) ), '..', '..', 'lib' ) ]
11
12
from logging import warning
13
14
import dramatis
15
16
class PingPong ( dramatis.Actor ):
17
18
    def __init__(self,name):
19
        self._name = name
20
21
    def pingpong(self,count,partner):
22
        if count == 0:
23
            print "%s: done" % self._name
24
        else:
25
            if count % 500 == 0 or count % 500 == 1:
26
                print "%s: pingpong %d" % ( self._name, count )
27
            dramatis.release( partner ).pingpong( count-1, self )
28
            time.sleep( 0.001 )
29
30
ping = PingPong( "ping" )
31
pong = PingPong( "pong" )
32
33
ping.pingpong( int(sys.argv[1]), pong )