1234567891011121314151617181920212223242526272829303132 |
- #!/usr/bin/env python
- from multiprocessing import Pool
- import os
- import time
- import random
- def worker(data):
- print "[%s] starting process, working on [%s]" % (os.getpid(), data)
- time.sleep(random.randrange(5))
- success = (random.random() > 0.2)
- if success:
- print "[%s] finished [%s] successfully" % (os.getpid(), data)
- else:
- print "[%s] error processing [%s]" % (os.getpid(), data)
- return success
- if __name__ == '__main__':
- mydata = ["the", "quick", "brown", "fox", "jumped",
- "over", "the", "lazy", "dog"]
- p = Pool(4) # defaults to number of CPUS
- # can also provide an initialization function
- # and arguments to it
- print "[%s] Pool created" % os.getpid()
- results = p.map(worker, mydata)
- print "[%s] Pool map completed" % os.getpid()
- print results
|