![](/img/trans.png)
[英]How to emulate multiprocessing.Pool.map() in AWS Lambda?
[英]How to stop multiprocessing.Pool.map on exception
當我在thread_function
內部引發異常時,它不會停止map
處理的 rest 。 我想阻止它。
def thread_function(n):
if n == 10:
raise Exception('Stop everything!')
pool = Pool(processes = 4)
pool.map(thread_function, range(1, 1000), chunksize = 1)
我希望在一個線程達到n == 10
后不再進行處理。
我不知道用map
直接執行此操作的方法,但是您可以像這樣監視async_map
...
from multiprocessing import Pool
import time
def thread_function(n):
if n == 10:
print('Raising Exception')
raise Exception('Stop everything!')
print(n)
time.sleep(0.1)
pool = Pool(processes = 4)
result = pool.map_async(thread_function, range(1, 1000), chunksize = 1)
while not result.ready():
if not result._success:
print('Exiting for failure')
pool.terminate()
pool.join()
break
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.