Use pipe worker for the main render process

This commit is contained in:
Kovid Goyal 2019-10-26 15:11:23 +05:30
parent 8972d63efd
commit facc4045d4
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 19 additions and 5 deletions

View File

@ -14,9 +14,11 @@ from hashlib import sha1
from calibre import walk
from calibre.constants import cache_dir, iswindows
from calibre.ptempfile import TemporaryFile
from calibre.srv.render_book import RENDER_VERSION
from calibre.utils.ipc.simple_worker import fork_job
from calibre.utils.ipc.simple_worker import start_pipe_worker
from calibre.utils.lock import ExclusiveFile
from calibre.utils.serialize import msgpack_dumps
from calibre.utils.short_uuid import uuid4
from polyglot.builtins import as_bytes, as_unicode, iteritems
@ -120,10 +122,16 @@ def prepare_convert(temp_path, key, st):
def do_convert(path, temp_path, key, instance):
tdir = os.path.join(temp_path, instance['path'])
fork_job('calibre.srv.render_book', 'render_for_viewer', args=(
with TemporaryFile('log.txt') as logpath, 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.stdin.write(msgpack_dumps((
path, tdir, {'size': instance['file_size'], 'mtime': instance['file_mtime'], 'hash': key},
), timeout=3000, no_output=True
)
)))
p.stdin.close()
if p.wait() != 0:
with lopen(logpath, 'rb') as logf:
raise Exception('Failed to convert book: {} with errors:\n{}'.format(
path, logf.read().decode('utf-8', 'replace')))
size = 0
for f in walk(tdir):
size += os.path.getsize(f)

View File

@ -861,5 +861,11 @@ def render_for_viewer(path, out_dir, book_hash):
)
def viewer_main():
stdin = getattr(sys.stdin, 'buffer', sys.stdin)
args = msgpack_loads(stdin.read())
render_for_viewer(*args)
if __name__ == '__main__':
render_for_viewer(sys.argv[-2], sys.argv[-1], None)