Python2.6以降。multiprocessingモジュール。まあos.fork。
17.2. multiprocessing — Process-based parallelism — Python 3.5.1 documentation
hello.py
#!/usr/bin/env python # coding: utf-8 from multiprocessing import Pool, cpu_count import os from time import sleep from datetime import datetime def hoge(v): print('pid: %d, %s, %d' % (os.getpid(), datetime.now(), v)) sleep(1) return v * v def main(): print('pid: %d' % os.getpid()) c = cpu_count() print('num of cpu: %d' % c) pool = Pool(c) result = pool.map_async(hoge, xrange(10)) print([val for val in result.get()]) if __name__ == '__main__': main()
実行結果
$ ./hello.py pid: 12541 num of cpu: 2 pid: 12543, 2009-01-01 04:40:43.142027, 0 pid: 12544, 2009-01-01 04:40:43.142173, 2 pid: 12543, 2009-01-01 04:40:44.142908, 1 pid: 12544, 2009-01-01 04:40:44.143424, 3 pid: 12543, 2009-01-01 04:40:45.144413, 4 pid: 12544, 2009-01-01 04:40:45.144954, 6 pid: 12543, 2009-01-01 04:40:46.145459, 5 pid: 12544, 2009-01-01 04:40:46.145909, 7 pid: 12543, 2009-01-01 04:40:47.146760, 8 pid: 12543, 2009-01-01 04:40:48.147565, 9 [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
正直まだあんまり理解できてない。