import os, subprocess, multiprocessing, shutil, os, sys, tempfile, atexit DIR = tempfile.mkdtemp(prefix='temp_') print 'CREATED TEMP DIRECTORY ' + DIR def clean_temp(): print 'ATEXIT: REMOVING TEMP DIRECTORY ' + DIR shutil.rmtree(DIR, ignore_errors=True) atexit.register(clean_temp) def task(msg): print msg def run_commands(commands): pool = multiprocessing.Pool(processes=2) pool.map(task, commands, chunksize=1) if __name__ == '__main__': run_commands(['task1', 'task2'])