|
3 | 3 |
|
4 | 4 | Handles the parsing, storage, and manipulation of 3D print file thumbnail images. |
5 | 5 | """ |
| 6 | +import asyncio |
6 | 7 | import base64 |
7 | 8 | from pathlib import Path |
8 | 9 | from typing import Optional |
@@ -119,6 +120,12 @@ def to_base64_data_url(self) -> Optional[str]: |
119 | 120 | base64_data = base64.b64encode(self._image_data).decode('ascii') |
120 | 121 | return f"data:image/png;base64,{base64_data}" |
121 | 122 |
|
| 123 | + @staticmethod |
| 124 | + def _write_file_sync(file_path: str, data: bytes) -> None: |
| 125 | + """Helper to write bytes to a file synchronously.""" |
| 126 | + with open(file_path, 'wb') as f: |
| 127 | + f.write(data) |
| 128 | + |
122 | 129 | async def save_to_file(self, file_path: Optional[str] = None) -> bool: |
123 | 130 | """ |
124 | 131 | Saves the thumbnail image data to a file. |
@@ -149,9 +156,9 @@ async def save_to_file(self, file_path: Optional[str] = None) -> bool: |
149 | 156 | print("ThumbnailInfo: No file path provided and no filename to generate one from") |
150 | 157 | return False |
151 | 158 |
|
152 | | - # Write the bytes to file |
153 | | - with open(file_path, 'wb') as f: |
154 | | - f.write(self._image_data) |
| 159 | + # Write the bytes to file in a separate thread to avoid blocking the event loop |
| 160 | + loop = asyncio.get_running_loop() |
| 161 | + await loop.run_in_executor(None, self._write_file_sync, file_path, self._image_data) |
155 | 162 |
|
156 | 163 | print(f"ThumbnailInfo: Saved thumbnail to {file_path}") |
157 | 164 | return True |
|
0 commit comments