#!/usr/bin/python # proc_run will run the given function in another process # A real process! Like, one with it's own memory space and such. # The function cannot take any arguments (use lambda) and will be able to return a bit of text (use pickle) # The result will appear in result_return[1]. result_return[0] indicates whether the function is done or not. from sys import exit from os import pipe, waitpid, fork, close, fdopen from thread import start_new_thread from time import sleep def wait_for_client(r,result_return,pid): result = r.read() result_return.append(result) waitpid(pid,0) result_return[0] = 1 # Function must be made using lambda def proc_run(function, result_return): if len(result_return) < 1: result_return.append(0) else: result_return[0] = 0 r, w = pipe() pid = fork() if pid: # parent close(w) r = fdopen(r) # r becomes a file object # We must start a pthread to wait for the client, and return start_new_thread(wait_for_client, (r,result_return,pid)) else: close(r) w = fdopen(w, 'w') sleep(0.01) w.write(function()) w.close() exit(0) return def exec_list(cmd, params, nthreads, delay=0): if nthreads <= 1: return [cmd(p) for p in params] results = [[0] for i in range(len(params))] for i,p in enumerate(params): proc_run(lambda : cmd(p), results[i]) if delay: sleep(delay) running_count = (i+1) - sum(r[0] for r in results[:i+1]) while running_count >= nthreads: sleep(0.1) running_count = (i+1) - sum(r[0] for r in results[:i+1]) done = False while not done: sleep(0.1) done = True for r in results: if not r[0]: done = False return [r[1] for r in results]