'The asyncio.sleep is "waking up" too early

I am writing a telegram bot, which has to work for numerous users simultaneously. The bot has to send messages at random time during a certain time frame. When the time on server is not in the chosen time frame, the function runs asyncio.sleep till the next day.

await message.answer("Hello")
now4 = datetime.now().strftime("%m/%d/%Y, %H:%M:%S")
now4 = datetime.strptime(now4, "%m/%d/%Y, %H:%M:%S")

add_date = full_date_start + timedelta(hours=24)
add_date = add_date - now4
add_date = add_date.seconds

till = add_date
print("Ok, Waiting for tomorrow")
print(till)
await asyncio.sleep(till)

Problem:

Sometimes the bot is waking up after approximately 10-30 seconds, running the rest of the function, this happens only from time to time. During the sleep, the bot can react to various commands (telegram commands), which work perfectly due to the asynchronous structure of the bot.

What can be the issue? Can the commands somehow break the asyncio.sleep() ?

The bot is built with an "aiogram" package, which is used for asynchronous bots, therefore asyncio.sleep() is used.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source