app: fix blocking I/O

Open and close static files to be read using the default
thread pool executor.

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Eric Callahan 2022-03-01 06:19:34 -05:00
parent 27466984df
commit 790d77756e
No known key found for this signature in database
GPG Key ID: 7027245FBBDDF59A
1 changed files with 5 additions and 1 deletions

View File

@ -46,6 +46,7 @@ if TYPE_CHECKING:
from confighelper import ConfigHelper
from klippy_connection import KlippyConnection as Klippy
from components.file_manager.file_manager import FileManager
from io import BufferedReader
import components.authorization
MessageDelgate = Optional[tornado.httputil.HTTPMessageDelegate]
AuthComp = Optional[components.authorization.Authorization]
@ -778,7 +779,8 @@ class FileRequestHandler(AuthorizedFileHandler):
start: Optional[int] = None,
end: Optional[int] = None
) -> AsyncGenerator[bytes, None]:
with open(abspath, "rb") as file:
file: BufferedReader = await evt_loop.run_in_thread(open, abspath, "rb")
try:
if start is not None:
file.seek(start)
if end is not None:
@ -798,6 +800,8 @@ class FileRequestHandler(AuthorizedFileHandler):
if remaining is not None:
assert remaining == 0
return
finally:
await evt_loop.run_in_thread(file.close)
@classmethod
def _get_cached_version(cls, abs_path: str) -> Optional[str]: