Skip to content

Commit 4a2d251

Browse files
committed
move to aiofiles @pcrespov
1 parent 72b467d commit 4a2d251

File tree

3 files changed

+30
-26
lines changed

3 files changed

+30
-26
lines changed

clients/python/setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"tenacity",
3131
"tqdm>=4.48.0",
3232
f"osparc_client=={VERSION}",
33+
"aiofiles",
3334
]
3435

3536
SETUP = dict(

clients/python/src/osparc/_api_files_api.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
FileUploadData,
2626
UploadedPart,
2727
)
28-
from tempfile import NamedTemporaryFile
2928
from urllib.parse import urljoin
29+
import aiofiles
3030
from ._utils import (
3131
DEFAULT_TIMEOUT_SECONDS,
3232
PaginationGenerator,
@@ -92,23 +92,26 @@ async def download_file_async(
9292
raise RuntimeError(
9393
f"destination_folder: {destination_folder} must be a directory"
9494
)
95-
downloaded_file = Path(
96-
NamedTemporaryFile(dir=destination_folder, delete=False).name
97-
)
98-
async with AsyncHttpClient(
99-
configuration=self.api_client.configuration, timeout=timeout_seconds
100-
) as session:
101-
url = urljoin(
102-
self.api_client.configuration.host, f"/v0/files/{file_id}/content"
103-
)
104-
async for response in await session.stream(
105-
"GET", url=url, auth=self._auth, follow_redirects=True
106-
):
107-
response.raise_for_status()
108-
with open(downloaded_file, mode="wb") as f:
95+
async with aiofiles.tempfile.NamedTemporaryFile(
96+
mode="wb",
97+
dir=f"{destination_folder.resolve()}"
98+
if destination_folder is not None
99+
else None,
100+
delete=False,
101+
) as downloaded_file:
102+
async with AsyncHttpClient(
103+
configuration=self.api_client.configuration, timeout=timeout_seconds
104+
) as session:
105+
url = urljoin(
106+
self.api_client.configuration.host, f"/v0/files/{file_id}/content"
107+
)
108+
async for response in await session.stream(
109+
"GET", url=url, auth=self._auth, follow_redirects=True
110+
):
111+
response.raise_for_status()
109112
async for chunk in response.aiter_bytes():
110-
f.write(chunk)
111-
return str(downloaded_file.resolve())
113+
await downloaded_file.write(chunk)
114+
return f"{downloaded_file.name}"
112115

113116
def upload_file(
114117
self,
@@ -130,7 +133,7 @@ async def upload_file_async(
130133
file = Path(file)
131134
if not file.is_file():
132135
raise RuntimeError(f"{file} is not a file")
133-
checksum: str = compute_sha256(file)
136+
checksum: str = await compute_sha256(file)
134137
for file_result in self._search_files(
135138
sha256_checksum=checksum, timeout_seconds=timeout_seconds
136139
):

clients/python/src/osparc/_utils.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
Solver,
1717
Study,
1818
)
19-
19+
import aiofiles
2020
from ._exceptions import RequestError
2121

2222
_KB = 1024 # in bytes
@@ -87,15 +87,15 @@ async def file_chunk_generator(
8787
bytes_read: int = 0
8888
file_size: int = file.stat().st_size
8989
while bytes_read < file_size:
90-
with open(file, "rb") as f:
91-
f.seek(bytes_read)
90+
async with aiofiles.open(file, "rb") as f:
91+
await f.seek(bytes_read)
9292
nbytes = (
9393
chunk_size
9494
if (bytes_read + chunk_size <= file_size)
9595
else (file_size - bytes_read)
9696
)
9797
assert nbytes > 0
98-
chunk = await asyncio.get_event_loop().run_in_executor(None, f.read, nbytes)
98+
chunk = await f.read(nbytes)
9999
yield chunk, nbytes
100100
bytes_read += nbytes
101101

@@ -109,16 +109,16 @@ async def _fcn_to_coro(callback: Callable[..., S], *args) -> S:
109109
return result
110110

111111

112-
def compute_sha256(file: Path) -> str:
112+
async def compute_sha256(file: Path) -> str:
113113
assert file.is_file()
114114
sha256 = hashlib.sha256()
115-
with open(file, "rb") as f:
115+
async with aiofiles.open(file, "rb") as f:
116116
while True:
117-
data = f.read(100 * _KB)
117+
data = await f.read(100 * _KB)
118118
if not data:
119119
break
120120
sha256.update(data)
121-
return sha256.hexdigest()
121+
return sha256.hexdigest()
122122

123123

124124
def dev_features_enabled() -> bool:

0 commit comments

Comments
 (0)