'Python GUI with Multithreading - how to manage threads

I am new to Python. I have been trying to develop a GUI based tool to monitor a set of databases. I want to pull data with multiple threads to make the DB reads faster. I found that threads can be managed using threading class or concurrent.futures class or using queue. In my tool there will be frequent DB reads and GUI will updated accordingly. My question is - what will be best option to work with for threading ? And how to manage life cycle of threads ?

I tried few example provided in different websites with following results.

  1. threads created using threading class are nicely updating the GUI. But I don't know how to manage 30 threads.
  2. Threads created using concurrent.futures.ThreadPoolExecutor are managed by the class. But it is updating the GUI after all the threads complete their task.


Solution 1:[1]

The thing with python threading is that there isn't really a proper way to stop a thread without stopping the entire execution. I am guessing your using threading or _thread

What I would do is create a list and have each function access a certain index of the list.
Process ID = Item in list.
thread 0 would be checking item 0 in list "running".
an example using _thread

import _thread
running = []
def task(id):
  global running
  while running[id]:
    #do something
#Create 5 tasks
for i in range(0,6):
  running.append(True)
  _thread.start_new_thread(task,(i,))
# Now lets stop tasks 2 and 4.
running[1] = False
running[3] = False
# After doing this the threads will end once code in while loop has finished
# To restart tasks 2 and 4
_thread.start_new_thread(task,(1,))
_thread.start_new_thread(task,(3,))

This is my rudimentary way of managing tasks.
It may or may not work for you.
I am not a professional. But it works.

Sources

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

Source: Stack Overflow

Solution Source
Solution 1