MCPcopy Index your code
hub / github.com/Tong89/smartNode / _simulation_loop

Method _simulation_loop

backend/core.py:586–644  ·  view source on GitHub ↗

仿真主循环

(self)

Source from the content-addressed store, hash-verified

584 self.simulation_thread.start()
585
586 def _simulation_loop(self):
587 """仿真主循环"""
588 last_time = time.time()
589 error_count = 0
590 max_consecutive_errors = 10
591
592 while self.running:
593 try:
594 current_real_time = time.time()
595 delta_real = current_real_time - last_time
596 last_time = current_real_time
597
598 # 仿真时间推进
599 delta_sim = delta_real * TIME_SCALE
600
601 with self.lock:
602 self.current_time += delta_sim
603
604 # 生成背景任务
605 if self.background_task_enabled:
606 self._generate_background_tasks()
607
608 # 更新资源利用率统计
609 self._update_resource_utilization()
610
611 # ⭐ 更新决策指标
612 self._update_decision_metrics()
613
614 # 更新传输任务
615 self._update_transmissions(delta_sim)
616
617 # 成功执行,重置错误计数
618 error_count = 0
619 time.sleep(0.01) # ⭐ 10ms更新间隔,提高动画流畅度
620
621 except Exception as e:
622 error_count += 1
623 import traceback
624 tb = traceback.format_exc()
625 _core_logger.error(
626 "仿真循环异常",
627 error=str(e),
628 error_count=error_count,
629 max_consecutive_errors=max_consecutive_errors,
630 traceback=tb,
631 )
632 # ⭐ 可观测:保留最近的循环异常(供调试接口查询),不静默吞掉
633 recent = getattr(self, "_loop_errors", [])
634 recent.append({"time": time.time(), "error": str(e), "traceback": tb})
635 self._loop_errors = recent[-20:]
636
637 if error_count >= max_consecutive_errors:
638 _core_logger.critical(
639 "连续错误超过阈值,仿真循环终止",
640 max_consecutive_errors=max_consecutive_errors,
641 )
642 break
643

Callers

nothing calls this directly

Calls 5

_update_transmissionsMethod · 0.95
appendMethod · 0.80

Tested by

no test coverage detected