您好,登錄后才能下訂單哦!
使用 ProcessPoolExecutor
from concurrent.futures import ProcessPoolExecutor, as_completed import random
斐波那契數列
當 n 大于 30 時拋出異常
def fib(n): if n > 30: raise Exception('can not > 30, now %s' % n) if n <= 2: return 1 return fib(n-1) + fib(n-2)
準備數組
nums = [random.randint(0, 33) for _ in range(0, 10)] ''' [13, 17, 0, 22, 19, 33, 7, 12, 8, 16] '''
方案一:submit
submit 輸出結果按照子進程執行結束的先后順序,不可控
with ProcessPoolExecutor(max_workers=3) as executor: futures = {executor.submit(fib, n):n for n in nums} for f in as_completed(futures): try: print('fib(%s) result is %s.' % (futures[f], f.result())) except Exception as e: print(e) ''' fib(13) result is 233. fib(17) result is 1597. fib(0) result is 1. fib(22) result is 17711. fib(19) result is 4181. can not > 30, now 33 fib(7) result is 13. fib(12) result is 144. fib(8) result is 21. fib(16) result is 987. '''
等價寫法:
with ProcessPoolExecutor(max_workers=3) as executor: futures = {} for n in nums: job = executor.submit(fib, n) futures[job] = n for job in as_completed(futures): try: re = job.result() n = futures[job] print('fib(%s) result is %s.' % (n, re)) except Exception as e: print(e) ''' fib(13) result is 233. fib(17) result is 1597. fib(0) result is 1. fib(22) result is 17711. can not > 30, now 33 fib(7) result is 13. fib(19) result is 4181. fib(8) result is 21. fib(12) result is 144. fib(16) result is 987. '''
方案二:map
map 輸出結果按照輸入數組的順序
缺點:某一子進程異常會導致整體中斷
with ProcessPoolExecutor(max_workers=3) as executor: try: results = executor.map(fib, nums) for num, result in zip(nums, results): print('fib(%s) result is %s.' % (num, result)) except Exception as e: print(e) ''' fib(13) result is 233. fib(17) result is 1597. fib(0) result is 1. fib(22) result is 17711. fib(19) result is 4181. can not > 30, now 33 '''
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。