mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Ensure worker process is killed on viewer exit
This commit is contained in:
parent
bcc9f16df2
commit
2da1f63821
@ -129,11 +129,24 @@ class ConversionFailure(ValueError):
|
|||||||
self, 'Failed to convert book: {} with error:\n{}'.format(book_path, worker_output))
|
self, 'Failed to convert book: {} with error:\n{}'.format(book_path, worker_output))
|
||||||
|
|
||||||
|
|
||||||
|
running_workers = []
|
||||||
|
|
||||||
|
|
||||||
|
def clean_running_workers():
|
||||||
|
for p in running_workers:
|
||||||
|
if p.poll() is None:
|
||||||
|
p.kill()
|
||||||
|
del running_workers[:]
|
||||||
|
|
||||||
|
|
||||||
def do_convert(path, temp_path, key, instance):
|
def do_convert(path, temp_path, key, instance):
|
||||||
tdir = os.path.join(temp_path, instance['path'])
|
tdir = os.path.join(temp_path, instance['path'])
|
||||||
|
p = None
|
||||||
|
try:
|
||||||
with TemporaryFile('log.txt') as logpath:
|
with TemporaryFile('log.txt') as logpath:
|
||||||
with open(logpath, 'w+b') as logf:
|
with open(logpath, 'w+b') as logf:
|
||||||
p = start_pipe_worker('from calibre.srv.render_book import viewer_main; viewer_main()', stdout=logf, stderr=logf)
|
p = start_pipe_worker('from calibre.srv.render_book import viewer_main; viewer_main()', stdout=logf, stderr=logf)
|
||||||
|
running_workers.append(p)
|
||||||
p.stdin.write(msgpack_dumps((
|
p.stdin.write(msgpack_dumps((
|
||||||
path, tdir, {'size': instance['file_size'], 'mtime': instance['file_mtime'], 'hash': key},
|
path, tdir, {'size': instance['file_size'], 'mtime': instance['file_mtime'], 'hash': key},
|
||||||
)))
|
)))
|
||||||
@ -142,6 +155,11 @@ def do_convert(path, temp_path, key, instance):
|
|||||||
with lopen(logpath, 'rb') as logf:
|
with lopen(logpath, 'rb') as logf:
|
||||||
worker_output = logf.read().decode('utf-8', 'replace')
|
worker_output = logf.read().decode('utf-8', 'replace')
|
||||||
raise ConversionFailure(path, worker_output)
|
raise ConversionFailure(path, worker_output)
|
||||||
|
finally:
|
||||||
|
try:
|
||||||
|
running_workers.remove(p)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
size = 0
|
size = 0
|
||||||
for f in walk(tdir):
|
for f in walk(tdir):
|
||||||
size += os.path.getsize(f)
|
size += os.path.getsize(f)
|
||||||
|
@ -28,7 +28,9 @@ from calibre.gui2.viewer.annotations import (
|
|||||||
merge_annotations, parse_annotations, save_annots_to_epub, serialize_annotations
|
merge_annotations, parse_annotations, save_annots_to_epub, serialize_annotations
|
||||||
)
|
)
|
||||||
from calibre.gui2.viewer.bookmarks import BookmarkManager
|
from calibre.gui2.viewer.bookmarks import BookmarkManager
|
||||||
from calibre.gui2.viewer.convert_book import prepare_book, update_book
|
from calibre.gui2.viewer.convert_book import (
|
||||||
|
clean_running_workers, prepare_book, update_book
|
||||||
|
)
|
||||||
from calibre.gui2.viewer.lookup import Lookup
|
from calibre.gui2.viewer.lookup import Lookup
|
||||||
from calibre.gui2.viewer.overlay import LoadingOverlay
|
from calibre.gui2.viewer.overlay import LoadingOverlay
|
||||||
from calibre.gui2.viewer.toc import TOC, TOCSearch, TOCView
|
from calibre.gui2.viewer.toc import TOC, TOCSearch, TOCView
|
||||||
@ -495,5 +497,6 @@ class EbookViewer(MainWindow):
|
|||||||
except Exception:
|
except Exception:
|
||||||
import traceback
|
import traceback
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
|
clean_running_workers()
|
||||||
return MainWindow.closeEvent(self, ev)
|
return MainWindow.closeEvent(self, ev)
|
||||||
# }}}
|
# }}}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user