language: Python (python 2.7.3)
date: 132 days 20 hours ago
link:
visibility: private
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/env python
import logging
from multiprocessing.dummy import Pool
 
debug = logging.getLogger(__name__).debug
 
def work(x_y):
    try:
        x, y = x_y # do some work here
        debug('got %r', x_y)
        return x / y, None
    except Exception as e:
        logging.getLogger(__name__).exception('work%r failed', x_y) 
        return None, e
 
def main():
    logging.basicConfig(level=logging.DEBUG,
                        format="%(levelname)s:%(threadName)s:%(asctime)s %(message)s")
 
    inputlist = [ (5,5),(10,4),(78,5),(87,2),(65,4),(10,10), (1,0), (0,1) ]
    pool = Pool(3)
 
    s = 0.
    for result, error in pool.imap_unordered(work, inputlist):
        if error is None:
           s += result
    print("sum=%s" % (s,))
    pool.close()
    pool.join()
 
if __name__ == "__main__":
   main()