multiprocessing module has similar API as in threading module. It works on Windows, Linux and OS/X.
The following code shows how to create and start several processes.
from multiprocessing import Process import os def info(title): print title print 'module name:', __name__ print 'parent process:', os.getppid() print 'process id:', os.getpid() def f(name): info('function f') print 'hello', name c = 0 for i in range(1,100000): for j in range(1, 100000): if i == j: c += 1 pass if __name__ == "__main__": info('main line') p = Process(target=f, args=('bob',)) q = Process(target=f, args=('sam',)) r = Process(target=f, args=('dan',)) s = Process(target=f, args=('david',)) q.start() p.start() r.start() s.start() p.join() q.join() r.join() s.join() pass
No comments:
Post a Comment