From b9e7dc61906dc3428da85ee27cd7807a1a552ae4 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 30 Aug 2014 11:02:36 +0530 Subject: [PATCH] Edit Book: Fix the Check Book tool leaking threads --- src/calibre/ebooks/oeb/polish/check/base.py | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/calibre/ebooks/oeb/polish/check/base.py b/src/calibre/ebooks/oeb/polish/check/base.py index 089de1390e83..84594c97cc69 100644 --- a/src/calibre/ebooks/oeb/polish/check/base.py +++ b/src/calibre/ebooks/oeb/polish/check/base.py @@ -8,6 +8,7 @@ from multiprocessing.pool import ThreadPool from functools import partial +from contextlib import closing from calibre import detect_ncpus as cpu_count @@ -31,13 +32,6 @@ def __str__(self): __repr__ = __str__ -class Worker(object): - - def __init__(self, func): - self.func = func - self.result = None - self.tb = None - def worker(func, args): try: result = func(*args) @@ -52,9 +46,10 @@ def run_checkers(func, args_list): num = cpu_count() pool = ThreadPool(num) ans = [] - for result, tb in pool.map(partial(worker, func), args_list): - if tb is not None: - raise Exception('Failed to run worker: \n%s' % tb) - ans.extend(result) + with closing(pool): + for result, tb in pool.map(partial(worker, func), args_list): + if tb is not None: + raise Exception('Failed to run worker: \n%s' % tb) + ans.extend(result) return ans