Home>

Every day at 20:00, the bot should iterate the database of users with a paid subscription and remove those whose subscription has expired. Everything seemed to be done, but I came across such a problem: in order for the moduleschedule(the module responsible for executing the function at a certain time) worked, the cycle must be active:

while True:
    schedule.run_pending()
    sleep(1)

So this loop blocks all other commands. Later I tried to rewrite the loop in an asynchronous way:

async def schedule_run():
    while True:
        schedule.run_pending()
        sleep(1)
if __name__== '__main__':
    asyncio.get_event_loop().run_until_complete(schedule_run())

but since the whole bot is written synchronously, it didn't help and the loop still blocks the rest of the bot's functionality. It will be difficult to rewrite the bot for the asynchronous model, to put it mildly, since it consists of 2100 lines of code. In general, I need to learn how to make the loop run in parallel from all other functions in the bot.

Here is the code snippet:

def db_check_pro_user(user_id):
    row, user_id, sub_type, date_buy, date_stop= db_get_pro_user(user_id)
    today= datetime.date.today()
    if today== date_stop:
        if sub_type== 1:
            bot.send_message(user_id, text="Text here", parse_mode="Markdown")
        elif sub_type== 2:
            bot.send_message(user_id, text="Text here", parse_mode="Markdown")
        db_del_pro_user(user_id)
def check_start():
    today= datetime.date.today()
    users= db_get_pro_users()
    for user in users:
        user_id= user[0]
        db_check_pro_user(user_id)
schedule.every().day.at("20:00").do(check_start)
while True:
    schedule.run_pending()
    sleep(1)

The bot is written in the telebot library.

sorry. did not read correctly. if the bot is synchronous, then the asynchronous function will not help in any way.

Anatoly Kritskiy2022-02-07 19:42:14

Do you have some kind of main loop of the bot itself?

Anatoly Kritskiy2022-02-07 19:42:14

No main loop

Beirym2022-02-07 19:42:14

the only crutch that I can offer, in the most frequently used function, or the function that you call with a certain frequency, is to insert a time check function, if the time is >20:00 and < 20:10 then run a user check. and let's write the date to the variable so that before checking the time, see if there was already a check today

Anatoly Kritskiy2022-02-07 19:42:14

Looked at the documentation on telebot. Since its waiting loop is inside the infinity_polling function, I decided to look at its sources. But when searching for it in the source, I found a ready-made example of its use in conjunction with shedule github.com/eternnoir/pyTelegramBotAPI/blob/…

Mike2022-02-07 19:42:14
  • Answer # 1

    Later I tried to rewrite the loop in an asynchronous way

    In this case, instead ofsleep()need to useawait asyncio.sleep().

  • Answer # 2

    Later I tried to rewrite the loop in an asynchronous way

    In this case, instead ofsleep()need to useawait asyncio.sleep().