I'm new to python, and I'm trying to create an application which runs a thread to create a GUI (with tkinter) and the idea is, while the GUI is shown to the user, in the background the program is making several API calls.
I have been trying different ways to make the program do what its supposed to do, but the only way I have achieved the desire behavior is using a thread for the GUI, and a thread which targets a function where threads are created to do the API calls.
I don't know if I am actually improving the performance of my application or just doing something meaningless.
My question is: Does it is actually a good practice to use threads inside a thread? Does it really improves the performance of a large operation like making several API calls?
I have tested the application with a few API calls like 15 or 20 and it works, but I expect to handle from 1000 to maybe 1,000,000 API calls.
Here is my code
#There is another function which calls get_all_data
#We are working in the main thread
#list_of_compounds and list_of_diseases are both string arrays
def get_all_data(list_of_compounds,list_of_diseases):
#create an object of class SearchData (where the API calls and the gui are defined)
obj=SearchData()
#Create a thread to handle a GUI (I'm using tkinter)
show_screen=threading.Thread(target=obj.gui)
#Create a thread to handle API calls
getData=threading.Thread(target=obj.startSearch,args=(list_of_compounds,list_of_diseases))
#start the thread to handle API calls
#I don't know why, but if I START THE show_screen THREAD FIRST, THE GUI I MADE IS NOT DISPLAYED UNTIL
#THE getData THREAD HAS FINISHED.
getData.start()
#start the gui
show_screen.start()
And here is the SearchData class:
class SearchData:
def __init__(self):
#Here is where I define the widgets of the GUI (definition)
# .
# .
# .
def gui(self):
#Here is where I display the GUI (implementation)
# .
# .
# .
def startSearch(self,compounds,diseases): #The compounds array or the diseases array are expected to have more
#than 10,000 elements
#Lists for the threads
compounds_threads=list()
disease_threads=list()
for item in compounds: #I'm creating a thread for each element in the compounds array
compound=threading.Thread(target=get_drug_data)
compounds_threads.append(compound) #save the thread in a list to use in the next for cycle (to join)
compound.start()
for item in enumerate(compounds_threads):
item.join()
for item in diseases: #I'm creating a thread for each element in the diseases array
disease=threading.Thread(target=get_disease_data)
disease_threads.append(disease) #save the thread in a list to use in the next for cycle (to join)
disease.start()
for item in enumerate(disease_threads):
item.join()
As you can see, the thread getData creates threads, but I don't know if it actually improves the performance. I have read some posts here in StackOverflow where people say that create threads inside a thread isn't a good idea because the threads would be sharing the same memory, resources, etc.