TheBeardedBerry
TheBeardedBerry

Reputation: 1764

Multi-threaded tools

I am still pretty new to python scripting and am trying to speed up and smooth out some of my tools by adding multi-threading support. I feel like I may be miss-understanding some of the workflow theory here so feel free to redirect my mindset if I should be doing this another way. Basically what I am trying to do is the following:

==================================================================
###Pseudo-code -- I apologize if this does not follow existing conventions
main_function()
    if do_stuff_function is not running:
        Thread1 = new thread running do_stuff_function

do_stuff_function(args)
    do some stuff
    wait for 60 seconds (possibly using time.sleep())
    end thread (tell main_function that the thread is done)
==================================================================
###Abridged code
def main(self):
      check = True
      index = 0
      While index < 5:
          if check == True:
              check = False
              thread1 = threading.Thread(target=self.doStuff, args=(self, index))



def do_stuff(self, index):
     ###Stuff happens here####

     ###iterate index and somehow return it (possibly a global variable)
     time.sleep(60)
     ###somehow end the thread (still not entirely sure how to do that)

===================================================================

Notes:

-- This tool has a gui and it all tends to lock up and freeze if i run the time.sleep() in the main loop which is why I figured that multithreading would be a good solution (feel free to correct me if that is wrong). Or possibly a different way to wait that doesn't freeze the entire thread while waiting.

-- The program freezes in the while loop, is there a way to do this check without having to loop, possibly something like a callback (like a simulated button press when the do_stuff() function ends)?

-- I am also looking to try and add some error checking to this, so based on the results of do_stuff() return different error codes (just something to keep in mind).

I apologize if that is not enough info; feel free to ask for more specific info if you need it. Really guys, I appreciate all the help I can get, I am just trying to get a solid understanding of this stuff! Thanks!

Upvotes: 3

Views: 497

Answers (1)

jcr
jcr

Reputation: 1015

threads have some overhead and they all share the same CPU core, threads are good for waiting for user input or downloading files and other things where you are i/o limited, if you need more computing power I would suggest you use the multiprocessing module (see Multiprocessing vs Threading Python)

another problem with threads is that they 'lock up' because of the Global Interpreter Lock, this is because only 1 thread is allowed to write to memory at any one time, the absurd result is that to many threads brings down your program because they are all waiting to access memory

Upvotes: 2

Related Questions