import multiprocessing


def runParallel(allrunFunction, nProc, params):
	""" Run the specified function in parallel given allocated no. processors.
	params is a list of permutations of input parameters to the allrun function
	which are to be evaluated; should be specified as a list of tuples (even
	if the function only accepts a single value). Outputs from the allrun function
	will be returned as a sorted list corresponding to each parameter combination.
	
	For clear stdout messages, any runBatch calls in the allrunFunc should use
	the showPid parameter set to True.
	"""
	
	# create a pool of processes
	pool = multiprocessing.Pool(processes=nProc)
	
	# execute
	results = [pool.apply_async(allrunFunction, args=p) for p in params]
	
	# retrieve return values
	output = [p.get() for p in results]
	
	return output