F
F
FieryDruid2018-09-10 19:28:27
Python
FieryDruid, 2018-09-10 19:28:27

Which library to choose and how to use for Telegram bot to run a function every 30 seconds for 5 minutes in a separate thread?

I have a bot in Telegram that needs to call one function every 30 seconds for 5 minutes after the user takes a certain action. Initially, I implemented this through .sleep()in a normal cycle, but in this case, the bot simply "dies" for this time, and for all users, which does not quite suit me. What is the best way to organize a call to this function so that it does not stop the bot and works quietly when called by several users (each has its own timer)?
As options, I thought about Threading and Schedule.
In the first option, it seems to me that it is not a good idea to run a thread per user, even for five minutes (What if there are a couple of dozen users?)
With the second option, it is not entirely clear how to stop the function call at the right time + will stopping one of the functions affect all the others launched from other users?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Max Payne, 2018-09-10
@FieryDruid

In my projects, I use a separate process for this, which receives data about tasks for the current minute from the database. Runs them multithreaded (multiprocessing.pool.ThreadPool), or multiprocessing (multiprocessing.Pool), depending on the task, and then "sleeps" for 60 seconds.

T
Teslaman, 2018-09-10
@Teslaman

You can use the aiogram asynchronous library

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question