summaryrefslogtreecommitdiffstats
path: root/build_tools/l7/larch0/cli/test.py
blob: 18e8067c94b6ed3e887d1d41f9901dffa5061db3 (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
#!/usr/bin/env python
#
# This is a little script for testing the dispatcher and termination of
# subprocesses. It spawns several levels of itself, each instance just
# outputs an incrementing count until it is terminated.

import sys, os, time, signal

try:
    import json as serialize
except:
    import simplejson as serialize

def out(text):
    sys.stdout.write(serialize.dumps(text) + '\n')
    sys.stdout.flush()

def sigint(num, frame):
    """A handler for SIGINT. Tidy up properly and quit.
    """
    out("!>INTERRUPTED %d" % level)
    exit(1)
signal.signal(signal.SIGINT, sigint)

level = 3
while level > 0:
    if os.fork():
        break
    time.sleep(1)
    level -= 1

sys.stderr.write('%d: pid=%d ppid=%d pgrp=%d\n' % (level,
        os.getpid(), os.getppid(), os.getpgrp()))
sys.stderr.flush()
time.sleep(5)

n = 0
while True:
    n += 1
    out('--%d: %04d' % (level, n))
    time.sleep(4)