Skip to content

Commit 8a16fb6

Browse files
committed
perf(Save): save concurrently files
1 parent ae426ae commit 8a16fb6

File tree

1 file changed

+42
-32
lines changed

1 file changed

+42
-32
lines changed

src/opengeodeweb_back/utils_functions.py

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import zipfile
66
from collections.abc import Callable
77
from typing import Any
8+
from concurrent.futures import ThreadPoolExecutor
89

910
# Third party imports
1011
import flask
@@ -175,38 +176,47 @@ def save_all_viewables_and_return_info(
175176
additional_files=additional_files,
176177
)
177178
data_path = create_data_folder_from_id(data_entry.id)
178-
saved_native_file_path = geode_functions.save(
179-
geode_object,
180-
data,
181-
data_path,
182-
"native." + data.native_extension(),
183-
)
184-
saved_viewable_file_path = geode_functions.save_viewable(
185-
geode_object, data, data_path, "viewable"
186-
)
187-
saved_light_viewable_file_path = geode_functions.save_light_viewable(
188-
geode_object, data, data_path, "light_viewable"
189-
)
190-
with open(saved_light_viewable_file_path, "rb") as f:
191-
binary_light_viewable = f.read()
192-
data_entry.native_file_name = os.path.basename(saved_native_file_path[0])
193-
data_entry.viewable_file_name = os.path.basename(saved_viewable_file_path)
194-
data_entry.light_viewable = os.path.basename(saved_light_viewable_file_path)
195-
196-
session = get_session()
197-
if session:
198-
session.commit()
199-
200-
return {
201-
"native_file_name": data_entry.native_file_name,
202-
"viewable_file_name": data_entry.viewable_file_name,
203-
"id": data_entry.id,
204-
"object_type": geode_functions.get_object_type(geode_object),
205-
"binary_light_viewable": binary_light_viewable.decode("utf-8"),
206-
"geode_object": data_entry.geode_object,
207-
"input_files": data_entry.input_file,
208-
"additional_files": data_entry.additional_files,
209-
}
179+
with ThreadPoolExecutor() as executor:
180+
native_future = executor.submit(
181+
geode_functions.save,
182+
geode_object,
183+
data,
184+
data_path,
185+
"native." + data.native_extension(),
186+
)
187+
viewable_future = executor.submit(
188+
geode_functions.save_viewable, geode_object, data, data_path, "viewable"
189+
)
190+
light_viewable_future = executor.submit(
191+
geode_functions.save_light_viewable,
192+
geode_object,
193+
data,
194+
data_path,
195+
"light_viewable",
196+
)
197+
saved_light_viewable_file_path = light_viewable_future.result()
198+
with open(saved_light_viewable_file_path, "rb") as f:
199+
binary_light_viewable = f.read()
200+
saved_native_file_path = native_future.result()
201+
saved_viewable_file_path = viewable_future.result()
202+
data_entry.native_file_name = os.path.basename(saved_native_file_path[0])
203+
data_entry.viewable_file_name = os.path.basename(saved_viewable_file_path)
204+
data_entry.light_viewable = os.path.basename(saved_light_viewable_file_path)
205+
206+
session = get_session()
207+
if session:
208+
session.commit()
209+
210+
return {
211+
"native_file_name": data_entry.native_file_name,
212+
"viewable_file_name": data_entry.viewable_file_name,
213+
"id": data_entry.id,
214+
"object_type": geode_functions.get_object_type(geode_object),
215+
"binary_light_viewable": binary_light_viewable.decode("utf-8"),
216+
"geode_object": data_entry.geode_object,
217+
"input_files": data_entry.input_file,
218+
"additional_files": data_entry.additional_files,
219+
}
210220

211221

212222
def generate_native_viewable_and_light_viewable_from_object(

0 commit comments

Comments
 (0)