Bug fix: asyncio.run cannot take asyncio.gather as coroutine

This commit is contained in:
2022-10-12 13:31:23 +02:00
parent 25abb80343
commit 12f3a2cc73
2 changed files with 12 additions and 10 deletions

View File

@@ -11,7 +11,7 @@ __author__ = "Davide Testa"
__email__ = "davide@davte.it" __email__ = "davide@davte.it"
__credits__ = ["Marco Origlia", "Nick Lee @Nickoala"] __credits__ = ["Marco Origlia", "Nick Lee @Nickoala"]
__license__ = "GNU General Public License v3.0" __license__ = "GNU General Public License v3.0"
__version__ = "2.8.1" __version__ = "2.8.2"
__maintainer__ = "Davide Testa" __maintainer__ = "Davide Testa"
__contact__ = "t.me/davte" __contact__ = "t.me/davte"

View File

@@ -3438,6 +3438,16 @@ class Bot(TelegramBot, ObjectWithDatabase, MultiLanguageObject):
loop.stop() loop.stop()
return return
@classmethod
async def run_preliminary_tasks(cls):
await asyncio.gather(
*[
preliminary_task
for bot in cls.bots
for preliminary_task in bot.preliminary_tasks
]
)
@classmethod @classmethod
def run(cls, local_host=None, port=None): def run(cls, local_host=None, port=None):
"""Run aiohttp web app and all Bot instances. """Run aiohttp web app and all Bot instances.
@@ -3454,15 +3464,7 @@ class Bot(TelegramBot, ObjectWithDatabase, MultiLanguageObject):
try: try:
loop = asyncio.new_event_loop() loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop) asyncio.set_event_loop(loop)
asyncio.run( loop.run_until_complete(cls.run_preliminary_tasks())
asyncio.gather(
*[
preliminary_task
for bot in cls.bots
for preliminary_task in bot.preliminary_tasks
]
)
)
except Exception as e: except Exception as e:
logging.error(f"{e}", exc_info=True) logging.error(f"{e}", exc_info=True)
for bot in cls.bots: for bot in cls.bots: