top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Making python code Multi Threaded

+1 vote
314 views

I am working on integration of multiple GUI (Tkinter) elements, such a progress bar, label update, etc, and throughout my research i discovered that i need to have Threading modules used to distribute the calls for GUI update and processing of my main App.

My Application is broken into multiple Classes(modules), and i wanted to hear your thought on the best practices to implement the Threading model.

I was thinking to create a new py-module, and reference all other modules/class to it, and create thread.start() objects, that would execute the GUI part, other will handle GUI Updates, and other - will be doing the processing.

Please share some of your thought on this approach, or maybe you may suggest a more effective way.

posted Sep 20, 2013 by Satish Mishra

Looking for an answer?  Promote on:
Facebook Share Button Twitter Share Button LinkedIn Share Button
Here is some CODE that i wrote to present the working case. my main program is split in multiple modules, but this structure should represent what i am trying to get.

    module name: multiProcessLauncher.py
    
    import multiprocessing
    import gui
    
    def main():
     jobs = []
     p = multiprocessing.Process(target=gui.basicGui)
     jobs.append(p)
     p.start()
    
    if __name__ == '__main__':
     main()
     pass
    
    Module Name: gui.py
    
    from Tkinter import *
    import tkMessageBox
    import Tkinter
    import multiProcessLauncher
    import action
    
    def basicGui():
     g = action.Action()
     print "GUI"
     processor = multiProcessLauncher
     name = processor.multiprocessing.current_process().name
     print name, "starting"
     print name, "exiting"
    
     top = Tk()
     button = Button(top, text = "Press Me", command = g.do_something)
     button.pack()
     top.mainloop()
    
    def main():
     pass
    if __name__ == "__main__":
     main()
    
    Module Name: action.py
    
    class Action(object):
     def __init__(self):
     self.text = "Running Action"
     def do_something(self):
     print self.text

I am trying to figure out how to make use of multiprocessing access the PRINT from the action.py using the GUI button. if you run the code and press the button, the console will read nothing, but as soon as you close the GUI, it spits out the text to console. I read about using Que, but i am not sure how to implement, could someone suggest how?

Similar Questions
+2 votes

write functions to read and write in a hash table in a multi threaded environment. Approach should give decent optimization in terms of time complexity.

+1 vote

I want my thread to be killed when I receive a particular message from Master.(I want my thread to stop whatever it is doing and come out.)

I tried few things but not working,
1) thread_name.exit()
2) thread_name.daemon = True

Any other way?

0 votes

I have a pool of worker threads, created like this:

threads = [MyThread(*args) for i in range(numthreads)]
for t in threads:
 t.start()

I then block until the threads are all done:

while any(t.isAlive() for t in threads):
 pass

Is that the right way to wait for the threads to be done? Should I stick a call to time.sleep() inside the while loop? If so, how long should I sleep? That's probably an unanswerable question, but some guidelines on
choosing the sleep time will be appreciated.

...