root / examples / pingpong / serial.py

1
#!/usr/bin/env python
2
3
import sys
4
import time
5
6
# cf. Scala by Example, Chapter 3
7
8
class PingPong ( object ):
9
10
    def __init__(self,name):
11
        self._name = name
12
13
    def pingpong(self,count,partner):
14
        if count == 0:
15
            print "%s: done" % self._name
16
        else:
17
            if count % 500 == 0 or count % 500 == 1:
18
                print "%s: pingpong %d" % ( self._name, count )
19
            partner.pingpong( count-1, self )
20
            time.sleep( 0.001 )
21
22
ping = PingPong( "ping" )
23
pong = PingPong( "pong" )
24
25
ping.pingpong( int(sys.argv[1]), pong )