#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import sys
import time
from multiprocessing import Process, cpu_count
#-----------------------------------------------------------------------
def log(msg):
print "[%s %d] %s" % (time.asctime(), os.getpid(), msg)
#-----------------------------------------------------------------------
def dummy_process():
import random
t = random.randint(0, 2)
log("Sleeping %d second(s)..." % t)
time.sleep(t)
log("Done, bye")
#-----------------------------------------------------------------------
def process_manager(total_procs, target, wait_time=0.2):
""" Always maintain a total of @total_procs running @target and
waiting for each thread to finish @wait_time second(s). """
procs = []
log("Maximum number of processes in pool is %d" % total_procs)
while 1:
if len(procs) < total_procs:
log("Starting process %d" % (len(procs)+1))
p = Process(target=target)
p.start()
procs.append(p)
log("Total of %d process(es) started" % len(procs))
else:
i = 0
for p in list(procs):
p.join(wait_time)
if not p.is_alive():
log("Process finished, deleting and starting a new one...")
del procs[i]
continue
i += 1
if __name__ == "__main__":
total_procs = cpu_count()*5
process_manager(total_procs, dummy_process)
Comments