0

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.

Henry Peregrino
  • 126
  • 1
  • 1
  • 8
  • It's impossible to comment without seeing your code and knowing what each API call is doing. How many API calls will you be making concurrently? What do you do with the result of each response? How do your threads synchronise? – Iain Shelvington Apr 22 '20 at 02:20
  • If your threads are performing cpu bound operations - probably not (because of the GIL). This is more nuanced though - here is a great [video](https://www.youtube.com/watch?v=Obt-vMVdM8s). We would have to see exactly what you are doing with your threads. Concurrency (maybe by way of `asyncio` seems like a better choice for the kind of programming you are doing.. – modesitt Apr 22 '20 at 02:21
  • 2
    For this there are thread pools, which do a large number of tasks with a limited number of threads. – Klaus D. Apr 22 '20 at 02:22
  • 1
    There is no such thing `threads inside a thread`, only `threads inside a process`. – acw1668 Apr 22 '20 at 04:13
  • I updated my question in order to add the code of my application. I am just saving the results of the API in a txt file. I think of concurrency as a good option to improve the performance of my program, because, if I do the API calls in the main thread, it blocks until each API call has been done, and it does each API call one by one. – Henry Peregrino Apr 22 '20 at 17:50
  • Read [multiprocessing-vs-threading-python](https://stackoverflow.com/questions/3044580) – stovfl Apr 22 '20 at 18:20

0 Answers0