MCPcopy Create free account
hub / github.com/AstrBotDevs/AstrBot / _load

Method _load

astrbot/core/core_lifecycle.py:279–315  ·  view source on GitHub ↗

加载事件总线和任务并初始化.

(self)

Source from the content-addressed store, hash-verified

277 asyncio.create_task(update_llm_metadata())
278
279 def _load(self) -> None:
280 """加载事件总线和任务并初始化."""
281 # 创建一个异步任务来执行事件总线的 dispatch() 方法
282 # dispatch是一个无限循环的协程, 从事件队列中获取事件并处理
283 event_bus_task = asyncio.create_task(
284 self.event_bus.dispatch(),
285 name="event_bus",
286 )
287 cron_task = None
288 if self.cron_manager:
289 cron_task = asyncio.create_task(
290 self.cron_manager.start(self.star_context),
291 name="cron_manager",
292 )
293 temp_dir_cleaner_task = None
294 if self.temp_dir_cleaner:
295 temp_dir_cleaner_task = asyncio.create_task(
296 self.temp_dir_cleaner.run(),
297 name="temp_dir_cleaner",
298 )
299
300 # 把插件中注册的所有协程函数注册到事件总线中并执行
301 extra_tasks = []
302 for task in self.star_context._register_tasks:
303 extra_tasks.append(asyncio.create_task(task, name=task.__name__)) # type: ignore
304
305 tasks_ = [event_bus_task, *(extra_tasks if extra_tasks else [])]
306 if cron_task:
307 tasks_.append(cron_task)
308 if temp_dir_cleaner_task:
309 tasks_.append(temp_dir_cleaner_task)
310 for task in tasks_:
311 self.curr_tasks.append(
312 asyncio.create_task(self._task_wrapper(task), name=task.get_name()),
313 )
314
315 self.start_time = int(time.time())
316
317 async def _task_wrapper(self, task: asyncio.Task) -> None:
318 """异步任务包装器, 用于处理异步任务执行中出现的各种异常.

Callers 1

startMethod · 0.95

Calls 5

_task_wrapperMethod · 0.95
dispatchMethod · 0.80
startMethod · 0.65
runMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected