pool

Keyboard Interrupts with python's multiprocessing Pool

与世无争的帅哥 提交于 2019-11-26 00:46:38
问题 How can I handle KeyboardInterrupt events with python\'s multiprocessing Pools? Here is a simple example: from multiprocessing import Pool from time import sleep from sys import exit def slowly_square(i): sleep(1) return i*i def go(): pool = Pool(8) try: results = pool.map(slowly_square, range(40)) except KeyboardInterrupt: # **** THIS PART NEVER EXECUTES. **** pool.terminate() print \"You cancelled the program!\" sys.exit(1) print \"\\nFinally, here are the results: \", results if __name__ =

Can&#39;t pickle <type &#39;instancemethod&#39;> when using multiprocessing Pool.map()

爷,独闯天下 提交于 2019-11-25 23:58:06
问题 I\'m trying to use multiprocessing \'s Pool.map() function to divide out work simultaneously. When I use the following code, it works fine: import multiprocessing def f(x): return x*x def go(): pool = multiprocessing.Pool(processes=4) print pool.map(f, range(10)) if __name__== \'__main__\' : go() However, when I use it in a more object-oriented approach, it doesn\'t work. The error message it gives is: PicklingError: Can\'t pickle <type \'instancemethod\'>: attribute lookup __builtin__