V
V
Viktor2020-02-09 22:43:46
Python
Viktor, 2020-02-09 22:43:46

Multithreading or a loop in python?

Good day, professionals!

I'm new to Python, I'm writing a simple bot for my needs and I had a dilemma, how to do it right:
a) put almost all the contents of the bot into a loop
b) try to solve the problem through flows

A little about the bot : the bot checks for the current availability of certain nodes that are listed in the settings file. What I would like: the bot in a cycle checks the availability of all nodes available in the settings and issues a one-time problem only if the device status has changed.
Below is the structure of the bot:

Подключение библиотек
Подключение клавиатуры с кнопками /http /ssl /rdp
Функция отправки сообщений всем участникам чата что бот запущен
Функция проверки файлов настроек
Функция проверки списков доступа к чату
# точка №1
Функция проверкп http/https узлов из файла настроек
Функция проверки SSL узлов из файла настроек
Функция  проверки RDP узлов из файла настроек
Функция обработки нажатия кнопки /http
  вызов функции  http/https и вывод в чат статуса доступности узлов
Функция обработки нажатия кнопки /ssl
  вызов функции  ssl и вывод в чат статуса доступности узлов
Функция обработки нажатия кнопки /rdp
  вызов функции  rdp и вывод в чат статуса доступности узлов
Функция проверки состояния всех узлов
  вызов функции  http/https	
  вызов функции  ssl
  вызов функции  rdp
  создание списка недоступных устройств/или изменение статуса и вывод в чат боту

# точка №3

bot.polling()
# точка №2

So, I thought that my Wishlist can be implemented like this:
a) if you enclose a list of functions between point #1 and #2 in an infinite loop
b) or at point #3 create a thread where you can call the function "Function for checking the status of all nodes" endlessly ?
How is it correct, and if you do it as in paragraph "b", then how is it better to implement it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MazAlVlad, 2020-02-10
@MazAlVlad

I don't know which is better. In python there are threads, there are processes. Both are manageable: they lock, unlock, queue up. The main difference is that the threads switch in time when the kernel is not involved: pause, output, and I don’t know exactly what else, there is some kind of understandable python switching logic. There are processes that use multi-core and are already running in parallel, not on the same core, like threads. It turns out a thread - emulation of parallel execution, a process - parallel execution under the condition of a multi-core processor.
Some systems may not support multiprocessing. While I'm learning how to use it.
The main code is also a thread, therefore, if you use multithreading, it will also have to be defined as a thread in order to manage it in the future.
https://webdevblog.ru/vvedenie-v-potoki-v-python/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question