1+ import asyncio
12import datetime
23import io
34import logging
45import os
56import re
6- import subprocess
77import warnings
88from collections import namedtuple
99from decimal import Decimal
@@ -165,7 +165,7 @@ def do_test(self, *args, **kwargs):
165165 return do_test
166166
167167
168- def make_png_thumbnail_for_instance (filepath , max_dimension ):
168+ async def make_png_thumbnail_for_instance (filepath , max_dimension ):
169169 """Abstract function for making a thumbnail for a PDF
170170
171171 See helper functions below for how to use this in a simple way.
@@ -184,14 +184,17 @@ def make_png_thumbnail_for_instance(filepath, max_dimension):
184184 filepath ,
185185 "-png" ,
186186 ]
187- p = subprocess .Popen (
188- command , close_fds = True , stdout = subprocess .PIPE , stderr = subprocess .PIPE
187+ p = await asyncio .create_subprocess_exec (
188+ * command ,
189+ close_fds = True ,
190+ stdout = asyncio .subprocess .PIPE ,
191+ stderr = asyncio .subprocess .PIPE ,
189192 )
190- stdout , stderr = p .communicate ()
193+ stdout , stderr = await p .communicate ()
191194 return stdout , stderr .decode ("utf-8" ), str (p .returncode )
192195
193196
194- def make_png_thumbnails (filepath , max_dimension , pages , directory ):
197+ async def make_png_thumbnails (filepath , max_dimension , pages , directory ):
195198 """Abstract function for making a thumbnail for a PDF
196199
197200 See helper functions below for how to use this in a simple way.
@@ -212,13 +215,13 @@ def make_png_thumbnails(filepath, max_dimension, pages, directory):
212215 "-png" ,
213216 f"{ directory .name } /thumb-{ page } " ,
214217 ]
215- p = subprocess . Popen (
216- command ,
218+ p = await asyncio . create_subprocess_exec (
219+ * command ,
217220 close_fds = True ,
218- stdout = subprocess .PIPE ,
219- stderr = subprocess .PIPE ,
221+ stdout = asyncio . subprocess .DEVNULL ,
222+ stderr = asyncio . subprocess .DEVNULL ,
220223 )
221- p . communicate ()
224+ await p . wait ()
222225
223226
224227def pdf_bytes_from_image_array (image_list , output_path ) -> None :
@@ -392,17 +395,22 @@ def log_sentry_event(
392395 logger .log (level , message , extra = extra , ** kwargs )
393396
394397
395- def strip_metadata_with_exiftool (path : str ) -> bool :
398+ async def strip_metadata_with_exiftool (path : str ) -> bool :
396399 """Strip metadata from a file in place using exiftool
397400
398401 :param path: Temporary file path
399402 :return: True if exiftool succeeded or False if exiftool failed
400403 """
401- result = subprocess .run (
402- ["exiftool" , "-all=" , "-overwrite_original" , path ],
403- capture_output = True ,
404- text = True ,
404+ process = await asyncio .create_subprocess_exec (
405+ "exiftool" ,
406+ "-all=" ,
407+ "-overwrite_original" ,
408+ path ,
409+ close_fds = True ,
410+ stdout = asyncio .subprocess .DEVNULL ,
411+ stderr = asyncio .subprocess .DEVNULL ,
405412 )
413+ result = await process .wait ()
406414
407415 # exiftool returns 0 = success
408- return result . returncode == 0
416+ return result == 0
0 commit comments