2 Commits

Author SHA1 Message Date
8f22fcf6af method start no now blocks main cycle 2025-11-01 21:35:25 +03:00
311870fd73 Changed the algorithm of the start method 2025-11-01 21:00:14 +03:00
3 changed files with 13 additions and 10 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.2"
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.logger.info("ConfigManager task created")
self.logger.info("ConfigManager starting and awaiting _run()")
await self._run()
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,13 @@ class MyApp(ConfigManager):
async def main(): async def main():
app = MyApp("config.yaml") app = MyApp("config.yaml")
app.start()
logger.info("App started") logger.info("App started")
await asyncio.sleep(20) await app.start()
await app.stop()
logger.info("App finished")
while True:
await asyncio.sleep(1)
if __name__ == "__main__": if __name__ == "__main__":
asyncio.run(main()) asyncio.run(main())