Edit Book: Fix the Check Book tool leaking threads

This commit is contained in:
Kovid Goyal 2014-08-30 11:02:36 +05:30
parent d758d88191
commit b9e7dc6190

View File

@ -8,6 +8,7 @@ __copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
from multiprocessing.pool import ThreadPool from multiprocessing.pool import ThreadPool
from functools import partial from functools import partial
from contextlib import closing
from calibre import detect_ncpus as cpu_count from calibre import detect_ncpus as cpu_count
@ -31,13 +32,6 @@ class BaseError(object):
__repr__ = __str__ __repr__ = __str__
class Worker(object):
def __init__(self, func):
self.func = func
self.result = None
self.tb = None
def worker(func, args): def worker(func, args):
try: try:
result = func(*args) result = func(*args)
@ -52,9 +46,10 @@ def run_checkers(func, args_list):
num = cpu_count() num = cpu_count()
pool = ThreadPool(num) pool = ThreadPool(num)
ans = [] ans = []
for result, tb in pool.map(partial(worker, func), args_list): with closing(pool):
if tb is not None: for result, tb in pool.map(partial(worker, func), args_list):
raise Exception('Failed to run worker: \n%s' % tb) if tb is not None:
ans.extend(result) raise Exception('Failed to run worker: \n%s' % tb)
ans.extend(result)
return ans return ans