summaryrefslogtreecommitdiff
path: root/texgen.py
diff options
context:
space:
mode:
authorKeuin <[email protected]>2023-06-03 15:57:56 +0800
committerKeuin <[email protected]>2023-06-03 15:57:56 +0800
commitd845692816079cf78ded919bc1175647576d8529 (patch)
tree603a507d4def49b8d1dfe1d5ca295a3c51803bb0 /texgen.py
parent7e43c9320a7eb15a60bee04ab41df053cd8f1dc7 (diff)
Use non-blocking file/subprocess IO
Diffstat (limited to 'texgen.py')
-rw-r--r--texgen.py49
1 files changed, 26 insertions, 23 deletions
diff --git a/texgen.py b/texgen.py
index 61d90f7..eda0e0b 100644
--- a/texgen.py
+++ b/texgen.py
@@ -1,10 +1,11 @@
import asyncio
+import asyncio.subprocess as subprocess
import contextlib
import hashlib
import os
import shutil
-import typing
-import subprocess
+
+from aiofile import async_open
@contextlib.contextmanager
@@ -40,26 +41,28 @@ class TexGenerator:
with temp_dir(os.path.join(self._temp_path, os.urandom(24).hex())) as workdir:
job_name = 'texput'
tex_file_path = os.path.join(workdir, f'{job_name}.tex')
- with open(tex_file_path, 'w', encoding='utf-8') as f:
- f.write(tex_source)
- with subprocess.Popen(
- [
- 'xelatex',
- '-interaction=nonstopmode',
- '-halt-on-error',
- tex_file_path,
- ],
- cwd=workdir,
- stdin=subprocess.PIPE,
- stdout=subprocess.PIPE,
- stderr=subprocess.STDOUT,
- text=True,
- shell=True,
- ) as proc:
- stdout, stderr = proc.communicate(input='', timeout=self._task_timeout)
- print('STDOUT', stdout)
- print('STDERR', stderr)
- if proc.returncode != 0:
- raise TexGenerationError(f'xelatex process exited with non-zero code {proc.returncode}')
+ async with async_open(tex_file_path, 'w', encoding='utf-8') as f:
+ await f.write(tex_source)
+ proc = await subprocess.create_subprocess_exec(
+ *[
+ 'xelatex',
+ '-interaction=nonstopmode',
+ '-halt-on-error',
+ tex_file_path,
+ ],
+ cwd=workdir,
+ stdin=subprocess.PIPE,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE,
+ )
+ try:
+ await asyncio.wait_for(proc.wait(), timeout=10)
+ except TimeoutError:
+ raise TexGenerationError('xelatex timed out')
+ stdout, stderr = [(await x.read()).decode() for x in (proc.stdout, proc.stderr)]
+ print('STDOUT', stdout)
+ print('STDERR', stderr)
+ if proc.returncode != 0:
+ raise TexGenerationError(f'xelatex process exited with non-zero code {proc.returncode}')
os.rename(os.path.join(workdir, f'{job_name}.pdf'), cache_file_path)
return cache_file_path