summaryrefslogtreecommitdiff
path: root/blobs-multiprocess/multiproc.py
blob: c367e04b7ca5f19676edc1085f1a74001a9828a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python

from argparse import ArgumentParser
from twisted.internet import reactor
from twisted.internet.protocol import ProcessProtocol
from twisted.python.failure import Failure


class BlobsServerProtocol(ProcessProtocol):

    def outReceived(self, data):
        if not isinstance(data, Failure):
            data = data.strip()
        if data:
            print(data)

    def errorReceived(self, data):
        if not isinstance(data, Failure):
            data = data.strip()
        if data:
            print(data)

    def processEnded(self, data):
        if not isinstance(data, Failure):
            data = data.strip()
        if data:
            print(data)

    # def processExited(self, data):
    #     print(data)


def parse_args():
    parser = ArgumentParser()
    parser.add_argument('--procs', type=int, default=4,
                        help="the number of processes to spawn")
    args = parser.parse_args()
    return args


def spawn_servers(procs):
    protocol = BlobsServerProtocol()
    children = []
    python = '/home/drebs/.virtualenvs/apps/bin/python'
    for port in range(8001, 8001 + procs):
        args = [python, './blobs-server.py', '/tmp/blobs', str(port)]
        child = reactor.spawnProcess(protocol, python, args)
        children.append(child)


def main():
    args = parse_args()
    spawn_servers(args.procs)
    reactor.run()


if __name__ == "__main__":
    main()