Create a pool of workers to run your routine:
http://docs.python.org/2/library/multiprocessing.html#using-a-pool-of-workers
In your case, maybe something like:
from multiprocessing import Pool import os def gitSync(repo): print "I am", repo, "and my cwd is:", os.getcwd() os.chdir(repo) print "I am", repo, "and my cwd is:", os.getcwd() if __name__ == '__main__': dir = os.getcwd() repos = [item for item in os.listdir(dir) if os.path.isdir(os.path.join(dir, item))] print repos pool = Pool(maxtasksperchild=1) pool.map(gitSync, repos) pool.close() pool.join()
Please note that the pool can debug the debugging a bit, as the parent usually doesnβt show much more than one of my children who died, so start it with a single thread first.
Edit: Well, that was interesting to evaluate - note the new argument to the pool maxtasksperchild=1 . This process is not rebooted between calls, so if you change the directory in one call, you are still in that directory when the process is reused. Here I solved it simply by telling the pool to kill processes after each call.
john:captcrunch john$ python foo.py ['.git', '.idea', 'fixtures', 'lib', 'obj', 'raw', 'tests'] I am .git and my cwd is: /Users/john/code/linz/src/captcrunch I am .git and my cwd is: /Users/john/code/linz/src/captcrunch/.git I am .idea and my cwd is: /Users/john/code/linz/src/captcrunch I am .idea and my cwd is: /Users/john/code/linz/src/captcrunch/.idea I am fixtures and my cwd is: /Users/john/code/linz/src/captcrunch I am fixtures and my cwd is: /Users/john/code/linz/src/captcrunch/fixtures I am lib and my cwd is: /Users/john/code/linz/src/captcrunch I am lib and my cwd is: /Users/john/code/linz/src/captcrunch/lib I am obj and my cwd is: /Users/john/code/linz/src/captcrunch I am obj and my cwd is: /Users/john/code/linz/src/captcrunch/obj I am raw and my cwd is: /Users/john/code/linz/src/captcrunch I am raw and my cwd is: /Users/john/code/linz/src/captcrunch/raw I am tests and my cwd is: /Users/john/code/linz/src/captcrunch I am tests and my cwd is: /Users/john/code/linz/src/captcrunch/tests
source share