fix: cleanup function and thread creation
This commit is contained in:
parent
4cc3c6efb2
commit
95f0df27a4
28
main.py
28
main.py
@ -1,6 +1,9 @@
|
|||||||
import os
|
import os
|
||||||
import logging
|
import logging
|
||||||
import uuid
|
import uuid
|
||||||
|
import time
|
||||||
|
import functools
|
||||||
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from simcrunner import Simc, JsonExport, Arguments, Profile
|
from simcrunner import Simc, JsonExport, Arguments, Profile
|
||||||
|
|
||||||
@ -15,7 +18,6 @@ from simcrunner.simc import HtmlExport
|
|||||||
logging.basicConfig(level=logging.INFO)
|
logging.basicConfig(level=logging.INFO)
|
||||||
# simc_path = os.path.join('tests', 'simc')
|
# simc_path = os.path.join('tests', 'simc')
|
||||||
simc_path = "./"
|
simc_path = "./"
|
||||||
runner = Simc(simc_path=simc_path)
|
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
app.mount("/static", StaticFiles(directory="templates/static"), name="static")
|
app.mount("/static", StaticFiles(directory="templates/static"), name="static")
|
||||||
@ -27,7 +29,7 @@ def read_root():
|
|||||||
return FileResponse(index_path)
|
return FileResponse(index_path)
|
||||||
|
|
||||||
@app.post("/sim/current_gear")
|
@app.post("/sim/current_gear")
|
||||||
async def simulate_current_gear(simcprofile: Annotated[str, Form()]):
|
def simulate_current_gear(simcprofile: Annotated[str, Form()]):
|
||||||
|
|
||||||
profile_path = create_sim_arguments(simcprofile)
|
profile_path = create_sim_arguments(simcprofile)
|
||||||
export_path = create_html_export()
|
export_path = create_html_export()
|
||||||
@ -36,17 +38,15 @@ async def simulate_current_gear(simcprofile: Annotated[str, Form()]):
|
|||||||
profile = Profile(profile_path)
|
profile = Profile(profile_path)
|
||||||
args = Arguments(profile, iterations=1000)
|
args = Arguments(profile, iterations=1000)
|
||||||
|
|
||||||
|
runner = Simc(simc_path=simc_path)
|
||||||
(runner
|
(runner
|
||||||
.add_args(args)
|
.add_args(args)
|
||||||
.add_args('target_error=0.05', threads=4)
|
.add_args('target_error=0.05', threads=4)
|
||||||
.add_args(html_export)
|
.add_args(html_export)
|
||||||
.run())
|
.run())
|
||||||
|
|
||||||
with open(export_path, 'r') as file:
|
response = read_file_with_lru_cache(export_path)
|
||||||
response = file.read().replace('\n', '')
|
remove_temp_files(profile_path, export_path)
|
||||||
|
|
||||||
os.remove(export_path)
|
|
||||||
os.remove(profile_path)
|
|
||||||
|
|
||||||
return HTMLResponse(response)
|
return HTMLResponse(response)
|
||||||
|
|
||||||
@ -69,3 +69,17 @@ def create_sim_arguments(profile_data: str):
|
|||||||
create_profile(profile_path, profile_data)
|
create_profile(profile_path, profile_data)
|
||||||
|
|
||||||
return profile_path
|
return profile_path
|
||||||
|
|
||||||
|
def remove_temp_files(profile_path: str, export_path: str):
|
||||||
|
time.sleep(1)
|
||||||
|
os.remove(export_path)
|
||||||
|
os.remove(profile_path)
|
||||||
|
|
||||||
|
@functools.lru_cache(maxsize=2)
|
||||||
|
def read_file_with_lru_cache(file_path):
|
||||||
|
# Read the file content
|
||||||
|
with open(file_path, 'r') as file:
|
||||||
|
file_content = file.read()
|
||||||
|
print(f"Reading from file: {file_path}")
|
||||||
|
|
||||||
|
return file_content
|
||||||
|
Loading…
x
Reference in New Issue
Block a user