Skip to content

Commit

Permalink
Edit Book: Fix the Check Book tool leaking threads
Browse files Browse the repository at this point in the history
  • Loading branch information
kovidgoyal committed Aug 30, 2014
1 parent d758d88 commit b9e7dc6
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions src/calibre/ebooks/oeb/polish/check/base.py
Expand Up @@ -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

Expand All @@ -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)
Expand All @@ -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

0 comments on commit b9e7dc6

Please sign in to comment.