single_case_run.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import sys
  2. import pandas as pd
  3. import numpy as np
  4. import requests
  5. from concurrent.futures import ThreadPoolExecutor, as_completed
  6. from functools import partial
  7. from pathlib import Path
  8. root_path = Path(__file__).resolve().parent.parent
  9. sys.path.append(str(root_path))
  10. from models.function import function
  11. from models.comfort import comfort
  12. from models.safety import safety
  13. from models.traffic import traffic
  14. import json
  15. from config import config
  16. from models.common import log
  17. log_path = config.LOG_PATH
  18. logger = log.get_logger(log_path)
  19. def traffic_run(data):
  20. # 创建 traffic 类的实例
  21. traffic_instance = traffic.SpeedingViolation(data)
  22. # traffic_instance = traffic.OvertakingViolation(data)
  23. # 调用实例方法 report_statistic,它不接受除 self 之外的参数
  24. try:
  25. traffic_result = traffic_instance.report_statistic()
  26. # traffic_result = traffic_instance.overtake_statistic()
  27. print("------traffic--------")
  28. result = {"traffic": traffic_result}
  29. print(result)
  30. return traffic_result
  31. except Exception as e:
  32. print(f"An error occurred in Traffic.report_statistic: {e}")
  33. def comfort_run(data):
  34. # 创建 Comfort 类的实例
  35. comfort_instance = comfort.Comfort(data)
  36. # 调用实例方法 report_statistic,它不接受除 self 之外的参数
  37. try:
  38. comfort_result = comfort_instance.report_statistic()
  39. # print('------comfort--------')
  40. # result = {'comfort': comfort_result}
  41. # print(result)
  42. return comfort_result
  43. except Exception as e:
  44. print(f"An error occurred in Comfort.report_statistic: {e}")
  45. def safety_run(data):
  46. # 创建 Comfort 类的实例
  47. safety_instance = safety.Safe(data)
  48. # 调用实例方法 report_statistic,它不接受除 self 之外的参数
  49. try:
  50. safety_result = safety_instance.report_statistic()
  51. # print('------safety--------')
  52. # result = {'safety': safety_result}
  53. # print(result)
  54. return safety_result
  55. except Exception as e:
  56. print(f"An error occurred in Safe.report_statistic: {e}")
  57. def single_case_run(data, case_name):
  58. # 创建线程池并启动线程
  59. with ThreadPoolExecutor(max_workers=1) as executor:
  60. futures = [
  61. # executor.submit(function, data, config),
  62. # executor.submit(safety_run, data),
  63. # executor.submit(comfort_run, data),
  64. executor.submit(traffic_run, data),
  65. ]
  66. results = [] # 用于存储程序返回的结果
  67. # 等待所有线程完成并获取结果(带异常处理)
  68. for future in as_completed(futures):
  69. try:
  70. result = future.result() # 获取返回结果
  71. results.append(result) # 将结果添加到列表中
  72. # print(f'创建线程池并启动线程结果{result}')
  73. except Exception as e:
  74. print(f"{case_name}: An error occurred in a thread: {e}")
  75. return results
  76. def single_case_statistic(result_list):
  77. single_case_result = {}
  78. for result in result_list:
  79. if isinstance(result, dict):
  80. single_case_result.update(result)
  81. return single_case_result
  82. if __name__ == "__main__":
  83. pass