From 3162d78fd26b208099c7409020ceb7b113468575 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 14 Feb 2014 08:50:12 +0530 Subject: [PATCH] Fix #1280040 [[FYI] - Left overs in temp](https://bugs.launchpad.net/calibre/+bug/1280040) --- src/calibre/gui2/tweak_book/main.py | 8 +++++++- src/calibre/gui2/tweak_book/preview.py | 3 +++ src/calibre/utils/ipc/simple_worker.py | 9 ++++++--- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/calibre/gui2/tweak_book/main.py b/src/calibre/gui2/tweak_book/main.py index 155a3e2537..20432924e5 100644 --- a/src/calibre/gui2/tweak_book/main.py +++ b/src/calibre/gui2/tweak_book/main.py @@ -6,7 +6,7 @@ from __future__ import (unicode_literals, division, absolute_import, __license__ = 'GPL v3' __copyright__ = '2013, Kovid Goyal ' -import sys, os, importlib +import sys, os, importlib, time from PyQt4.Qt import QIcon @@ -67,6 +67,12 @@ def _run(args, notify=None): if len(args) > 1: main.boss.open_book(args[1], edit_file=opts.edit_file, clear_notify_data=False) app.exec_() + # Ensure that the parse worker has quit so that temp files can be deleted + # on windows + st = time.time() + from calibre.gui2.tweak_book.preview import parse_worker + while parse_worker.is_alive() and time.time() - st < 120: + time.sleep(0.1) def main(args=sys.argv): _run(args) diff --git a/src/calibre/gui2/tweak_book/preview.py b/src/calibre/gui2/tweak_book/preview.py index c54f369465..e2632aa69a 100644 --- a/src/calibre/gui2/tweak_book/preview.py +++ b/src/calibre/gui2/tweak_book/preview.py @@ -155,6 +155,9 @@ class ParseWorker(Thread): def clear(self): self.parse_items.clear() + def is_alive(self): + return Thread.is_alive(self) or self.worker.is_alive() + parse_worker = ParseWorker() # }}} diff --git a/src/calibre/utils/ipc/simple_worker.py b/src/calibre/utils/ipc/simple_worker.py index 28a059283a..f21e44c823 100644 --- a/src/calibre/utils/ipc/simple_worker.py +++ b/src/calibre/utils/ipc/simple_worker.py @@ -58,6 +58,8 @@ class OffloadWorker(object): self.listener = listener self.worker = worker self.conn = None + self.kill_thread = t = Thread(target=self.worker.kill) + t.daemon = True def __call__(self, module, func, *args, **kwargs): if self.conn is None: @@ -73,13 +75,14 @@ class OffloadWorker(object): traceback.print_exc() finally: self.conn = None - t = Thread(target=self.worker.kill) - t.daemon = True try: os.remove(self.worker.log_path) except: pass - t.start() + self.kill_thread.start() + + def is_alive(self): + return self.worker.is_alive or self.kill_thread.is_alive() def communicate(ans, worker, listener, args, timeout=300, heartbeat=None, abort=None):