M
M
Meksvinz2018-03-25 20:42:01
Python
Meksvinz, 2018-03-25 20:42:01

Telegram bot. How to implement parallel execution of tasks?

In general, I am writing a gaming telegram bot and ran into a problem. I want to implement the mechanics of moving from point A to point B, the transition takes some real time, say five minutes, and when these five minutes expire, the bot should send a notification to the user about arrival at the place (at the same time, other users can use the bot). Obviously, you need something that constantly checks against the value of the time allotted for the transition from A to B (the value in the database) and sends notifications to users when it expires. The first thing that came to mind was flows. But after all, if the bot terminates (the parent process), then all threads (the thread with the value check function) also terminate their work?
I ask you to suggest the direction in which it is worth moving in terms of solving this problem, thanks

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Ranc58, 2018-03-26
@Meksvinz

I implemented this in this way:
The database stores the necessary values, once a minute (for example) a task is launched in celery, which searches the database for IDs of people to whom the bot should send some kind of notification. If there are any, then the list of these IDs is transmitted to the bot via the API, and the bot arranges the mailing list.
The bot itself was implemented according to the following scheme:
https://github.com/eternnoir/pyTelegramBotAPI/blob...
just added one more handle that accepts a POST request with a list of user IDs.

G
Gasoid, 2018-03-26
@Gasoid

1) do not use threads in python
2) use celery, huey, rq for queues (the best way)
4) run several workers / processes
3) you can use asyncio, then you need to completely redo the program for asynchronous programming, do not use time.sleep and blocking operations

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question