Answer the question
In order to leave comments, you need to log in
Help for the uninformed with async in python. Is it possible to fix discord bot freezing with asyncio?
Hello
, I'm a beginner programmer, so I ask you not to yell at me and my shit code (well, sorry for the mistakes, I don't know Russian well in writing).
The essence of the problem.
I'm writing a discord bot using discord py which in turn uses asyncio. Before that, I didn’t think much about how async / await works and therefore I wrote code with async before def and await before the bot action, just because it’s necessary.
Example:
But now the bot has a functionality that downloads photos from Instagram using the instaloader library, and at the time of loading all the content, the bot freezes until the function is executed.
@tasks.loop(seconds=180)
async def parse_stories():
for profile in insta.profiles:
# функция get_new_downloaded возвращает список файлов либо False если нету изменений
# в качестве аргументов принимает путь к папке с файлами, функцию и профиль человека
new_stories = get_new_downloaded(f'./{profile} stories/',
insta.download_all_stories_by_profile, insta.profiles[profile])
print(profile)
if new_stories != False:
# сервер и канал куда будут отправляться все файлы
guild = client.get_guild(693165230934458380)
channel = guild.get_channel(729607318534225921)
for story in new_stories:
await channel.send(file=discord.File(f'./{profile} stories/{story}'))
logger.info(f'Bot send stories success! ./{profile} stories/{story}')
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question