Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

executable file 54 lines (39 sloc) 0.928 kb
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
#!/usr/bin/python

import time
from multiprocessing import Process
import signal
import sys
import random

from command import Command

procs = []

def worker():

    c = Command('for i in $(seq 30); do echo $i; sleep 1; done', setpgrp=True)

    def sigint(s, f):
        print "terminating %d" % c.pid
        c.terminate()
        sys.exit(1)

    signal.signal(signal.SIGINT, sigint)

    def cb(c, buf):
        if buf:
            sys.stdout.write(buf)
            sys.stdout.flush()

        return True

    c.read(cb)
    c.wait()

for i in range(100):
    p = Process(target=worker)
    p.start()
    procs.append(p)

# the code below is sufficient
signal.signal(signal.SIGINT, SIG_IGN)

while True:
    running = 0
    for proc in procs:
        if proc.is_alive():
            running += 1
        else:
            proc.join()

    print "RUNNING: %d" % running
    if not running:
        break

    time.sleep(1)
Something went wrong with that request. Please try again.