Skip to content

Commit 16ada33

Browse files
committed
threading for disk calls
1 parent 2546cfa commit 16ada33

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/py/reactpy/reactpy/backend/asgi.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -242,25 +242,28 @@ async def file_response(scope, send, file_path: Path) -> None:
242242
"""Send a file in chunks."""
243243

244244
# Make sure the file exists
245-
if not os.path.exists(file_path):
245+
if not await asyncio.to_thread(os.path.exists, file_path):
246246
await simple_response(send, 404, "File not found.")
247247
return
248248

249249
# Make sure it's a file
250-
if not os.path.isfile(file_path):
250+
if not await asyncio.to_thread(os.path.isfile, file_path):
251251
await simple_response(send, 400, "Not a file.")
252252
return
253253

254254
# Check if the file is already cached by the client
255255
modified_since = await get_val_from_header(scope, b"if-modified-since")
256-
if modified_since and modified_since > os.path.getmtime(file_path):
256+
if modified_since and modified_since > await asyncio.to_thread(
257+
os.path.getmtime, file_path
258+
):
257259
await simple_response(send, 304, "Not modified.")
258260
return
259261

260262
# Get the file's MIME type
261263
mime_type = (
262-
DEFAULT_MIME_TYPES.get(file_path.rsplit(".")[1], None)
263-
or mimetypes.guess_type(file_path, strict=False)[0]
264+
MIME_TYPES.get(file_path.rsplit(".")[1], None)
265+
# Fallback to guess_type to allow for the user to define custom MIME types on their system
266+
or (await asyncio.to_thread(mimetypes.guess_type, file_path, strict=False))[0]
264267
)
265268
if mime_type is None:
266269
mime_type = "text/plain"

src/py/reactpy/reactpy/backend/mimetypes.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
DEFAULT_MIME_TYPES = {
1+
"""
2+
We ship our own mime types to ensure consistent behavior across platforms.
3+
This dictionary is based on: https://github.com/micnic/mime.json
4+
"""
5+
6+
MIME_TYPES = {
27
"123": "application/vnd.lotus-1-2-3",
38
"1km": "application/vnd.1000minds.decision-model+xml",
49
"3dml": "text/vnd.in3d.3dml",

0 commit comments

Comments
 (0)