Run manually simulation in thread
This commit is contained in:
39
main.py
39
main.py
@@ -28,6 +28,8 @@ import simulation
|
||||
import globals
|
||||
import os
|
||||
import logging
|
||||
import threading
|
||||
import time
|
||||
from logging.handlers import TimedRotatingFileHandler
|
||||
|
||||
JUNCTION = 0
|
||||
@@ -2793,6 +2795,24 @@ class Run_Simulation_Manually_by_Date(BaseModel):
|
||||
name: str
|
||||
simulation_date: str
|
||||
|
||||
def run_simulation_manually_by_date(network_name: str, simulation_date: str) -> None:
|
||||
# 循环生成96个时间点(15分钟间隔)
|
||||
for i in range(96):
|
||||
# 计算当前时间偏移
|
||||
time_offset = timedelta(minutes=15 * i)
|
||||
|
||||
# 生成完整时间对象
|
||||
current_time = base_date + time_offset
|
||||
|
||||
# 格式化成ISO8601带时区格式
|
||||
iso_time = current_time.strftime("%Y-%m-%dT%H:%M:%S") + "+08:00"
|
||||
|
||||
## 执行函数调用
|
||||
simulation.run_simulation(
|
||||
name=network_name,
|
||||
simulation_type="realtime",
|
||||
modify_pattern_start_time=iso_time
|
||||
)
|
||||
|
||||
@app.post("/runsimulationmanuallybydate/")
|
||||
async def fastapi_run_simulation_manually_by_date(data: Run_Simulation_Manually_by_Date) -> None:
|
||||
@@ -2822,23 +2842,8 @@ async def fastapi_run_simulation_manually_by_date(data: Run_Simulation_Manually_
|
||||
|
||||
base_date = datetime.strptime(item['simulation_date'], "%Y-%m-%d")
|
||||
|
||||
# 循环生成96个时间点(15分钟间隔)
|
||||
for i in range(96):
|
||||
# 计算当前时间偏移
|
||||
time_offset = timedelta(minutes=15 * i)
|
||||
|
||||
# 生成完整时间对象
|
||||
current_time = base_date + time_offset
|
||||
|
||||
# 格式化成ISO8601带时区格式
|
||||
iso_time = current_time.strftime("%Y-%m-%dT%H:%M:%S") + "+08:00"
|
||||
|
||||
## 执行函数调用
|
||||
simulation.run_simulation(
|
||||
name=item['name'],
|
||||
simulation_type="realtime",
|
||||
modify_pattern_start_time=iso_time
|
||||
)
|
||||
thread = threading.Thread(target=lambda: run_simulation_manually_by_date(item['name'], item['simulation_date']))
|
||||
thread.start()
|
||||
|
||||
############################################################
|
||||
# real_time api 37.5
|
||||
|
||||
Reference in New Issue
Block a user