Changed the algorithm of the start method

This commit is contained in:
2025-11-01 21:00:14 +03:00
parent ffd758d9a4
commit 311870fd73
3 changed files with 9 additions and 8 deletions

View File

@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
[project] [project]
name = "config_manager" name = "config_manager"
version = "1.2.0" version = "1.2.1"
description = "Config manager for building applications" description = "Config manager for building applications"
authors = [ authors = [
{ name = "Aleksei Zosimov", email = "lesha.spb@gmail.com" } { name = "Aleksei Zosimov", email = "lesha.spb@gmail.com" }

View File

@@ -104,21 +104,21 @@ class ConfigManager:
finally: finally:
self.logger.info("ConfigManager stopped") self.logger.info("ConfigManager stopped")
def start(self) -> None: async def start(self) -> None:
"""Запускает менеджер конфигурации в текущем event loop"""
if self._task is not None and not self._task.done(): if self._task is not None and not self._task.done():
self.logger.warning("ConfigManager is already running") self.logger.warning("ConfigManager is already running")
return return
try: try:
self._loop = asyncio.get_running_loop() self._loop = asyncio.get_running_loop()
except RuntimeError: except RuntimeError:
self.logger.error("start() must be called from within an async context") self.logger.error("start() must be called from within an async context")
raise raise
self._task = self._loop.create_task(self._run()) self._task = self._loop.create_task(self._run())
self.logger.info("ConfigManager task created") self.logger.info("ConfigManager task created")
async def stop(self) -> None: async def stop(self) -> None:
"""Останавливает менеджер конфигурации и ожидает завершения""" """Останавливает менеджер конфигурации и ожидает завершения"""
if self._task is None: if self._task is None:

View File

@@ -21,10 +21,11 @@ class MyApp(ConfigManager):
async def main(): async def main():
app = MyApp("config.yaml") app = MyApp("config.yaml")
app.start() await app.start()
logger.info("App started") logger.info("App started")
await asyncio.sleep(20)
await app.stop() while True:
await asyncio.sleep(1)
if __name__ == "__main__": if __name__ == "__main__":
asyncio.run(main()) asyncio.run(main())