From 1adc0406a3625e1cc4e1630dba60d8f095efc2eb Mon Sep 17 00:00:00 2001 From: Martin Turoci Date: Thu, 12 Jan 2023 14:19:49 +0100 Subject: [PATCH] feat: Make async upload_dir non-blocking. #982 --- py/h2o_wave/core.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/py/h2o_wave/core.py b/py/h2o_wave/core.py index 70a988da93..686dc609ab 100644 --- a/py/h2o_wave/core.py +++ b/py/h2o_wave/core.py @@ -922,10 +922,7 @@ async def upload_dir(self, directory: str) -> str: else: args = ['rsync', '-a', os.path.join(directory, '.'), dst] - _, err = subprocess.Popen(args, stderr=subprocess.PIPE).communicate() - if err: - raise ValueError(err.decode()) - return [f'{_base_url}_f/{uuid}'] + return [await _copy_in_subprocess(args, uuid)] except Exception as e: print(f'Error during local copy, falling back to HTTP upload: {e}') @@ -976,7 +973,7 @@ async def upload(self, files: List[str]) -> List[str]: else: args = ['cp', f, dst] - tasks.append(asyncio.create_task(copy_in_subprocess(args, uuid, f))) + tasks.append(asyncio.create_task(_copy_in_subprocess(args, uuid, f))) return await asyncio.gather(*tasks) except Exception as e: @@ -1079,14 +1076,17 @@ async def proxy(self, method: str, url: str, headers: Optional[Dict[str, List[st raise ServiceError(f'Proxy request failed (code={res.status_code}): {res.text}') -async def copy_in_subprocess(args: List[str], uuid: str, f: str) -> str: +async def _copy_in_subprocess(args: List[str], uuid: str, f='') -> str: p = await asyncio.create_subprocess_exec(*args, stderr=subprocess.PIPE, stdout=subprocess.DEVNULL) _, err = await p.communicate() if err: raise ValueError(err.decode()) - return f'{_base_url}_f/{uuid}/{os.path.basename(f)}' + if f: + return f'{_base_url}_f/{uuid}/{os.path.basename(f)}' + else: + return f'{_base_url}_f/{uuid}' def _get_files_in_directory(directory: str, files: List[str]) -> List[str]: