~ntt-pf-lab/nova/monkey_patch_notification

« back to all changes in this revision

Viewing changes to vendor/Twisted-10.0.0/doc/core/howto/listings/process/process.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
# Copyright (c) 2009-2010 Twisted Matrix Laboratories.
 
4
# See LICENSE for details.
 
5
 
 
6
from twisted.internet import protocol
 
7
from twisted.internet import reactor
 
8
import re
 
9
 
 
10
class MyPP(protocol.ProcessProtocol):
 
11
    def __init__(self, verses):
 
12
        self.verses = verses
 
13
        self.data = ""
 
14
    def connectionMade(self):
 
15
        print "connectionMade!"
 
16
        for i in range(self.verses):
 
17
            self.transport.write("Aleph-null bottles of beer on the wall,\n" +
 
18
                                 "Aleph-null bottles of beer,\n" +
 
19
                                 "Take one down and pass it around,\n" +
 
20
                                 "Aleph-null bottles of beer on the wall.\n")
 
21
        self.transport.closeStdin() # tell them we're done
 
22
    def outReceived(self, data):
 
23
        print "outReceived! with %d bytes!" % len(data)
 
24
        self.data = self.data + data
 
25
    def errReceived(self, data):
 
26
        print "errReceived! with %d bytes!" % len(data)
 
27
    def inConnectionLost(self):
 
28
        print "inConnectionLost! stdin is closed! (we probably did it)"
 
29
    def outConnectionLost(self):
 
30
        print "outConnectionLost! The child closed their stdout!"
 
31
        # now is the time to examine what they wrote
 
32
        #print "I saw them write:", self.data
 
33
        (dummy, lines, words, chars, file) = re.split(r'\s+', self.data)
 
34
        print "I saw %s lines" % lines
 
35
    def errConnectionLost(self):
 
36
        print "errConnectionLost! The child closed their stderr."
 
37
    def processExited(self, reason):
 
38
        print "processExited, status %d" % (reason.value.exitCode,)
 
39
    def processEnded(self, reason):
 
40
        print "processEnded, status %d" % (reason.value.exitCode,)
 
41
        print "quitting"
 
42
        reactor.stop()
 
43
 
 
44
pp = MyPP(10)
 
45
reactor.spawnProcess(pp, "wc", ["wc"], {})
 
46
reactor.run()