0

I'm new in async programing. I have a python program that should activate some telegram clients at the same time.

Now it works in 'for':

    for user in wait_activation:
        user_chat_id = user.chat_id
        phone_number = user.phone_number
        client = Client(
            f'{user_chat_id}',
            api_id=self.api_id,
            api_hash=self.api_hash,
            phone_number=phone_number,
            phone_code_handler=await self.get_confirmation_code(
                client_id=user_chat_id
            )
        )

I have a custom phone code handler, that waits until user send his confirmation code to Telegram bot:

async def get_confirmation_code(self, client_id):
    async def _stab():
        while True:
            wrong_code = await self.redis.smembers('set:code_entered')
            if client_id in wrong_code:
                await asyncio.sleep(0)
                continue
            code = await self.redis.hget(
                'hash:id_conf_code',
                client_id
            )
            if code:
                await self.redis.hdel('hash:id_conf_code', client_id)
                await self.redis.sadd('set:code_entered', client_id)
                break
            await asyncio.sleep(1)
        return code

    return _stab

The problem is that if more then one try to activate client, the program is stoped until the previous person enters the code. Is there any ways to fix it?

I thought about some create_task but I don't find any ways to create many tasks inside 'for' loop

I apologize for bad wording and may be stupid question. I'm completely newbie

Dolise
  • 1
  • Does this answer your question? [How to use \`async for\` in Python?](https://stackoverflow.com/questions/56161595/how-to-use-async-for-in-python) – Woodford Aug 16 '21 at 20:38

0 Answers0