D
D
Denta2020-07-12 16:12:44
Python
Denta, 2020-07-12 16:12:44

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: KDWtNTQ.png

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}')


Is it possible to fix this using asynchrony and what articles will help me to solve this problem

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2020-07-12
@Denta

Just running the synchronous code will block the event loop, but the code can be run in a separate thread or process via the executor https://docs.python.org/3/library/asyncio-eventloo...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question