TransWikia.com

How to use multiprocessing with GUI and backend classes?

Stack Overflow Asked by ggokturk on August 17, 2020

I have a script which contains GUI and backend part. Briefly, you can see the summary of code below

class GUI():
    def __init__(self):
        self.initUi()
        self.clickedButton()


    def initUI(self):
        ... elements... buttons etc.
    
    
    def clickedButton(self):
        button.clicked.connect(Backend())

class Backend():
    def __init__(self):
         self.pool()

    def do_something1(self):
        ...
    def do_something2(self,x,y):
        print(x+y)
    
    def thread(self,x,y):
        ...do_something1...
        threading.Thread(target = self.do_something2(), args=(self,x,y)).start()

    def pool():
        p = Pool(processes=5)
        items=[(0,123),(123,246),(246,369),(369,492),(492,615)]
    
        p.starmap(self.thread, items)

if __name__ == '__main__':
   import sys
   from multiprocessing import Pool
   import threading
   ex  = GUI()
   sys.exit(app.exec_())

My code normally working well. But I want to do some works in parallel to speed up the algorithm. I know it is looking messy and something can be missing. I am apologizing for this. Mainly, I want to create 5 worker or more to run in parallel. So thread funcion in backend class work 5 times with different arguments that I defined in pool function basicly. When user clicked the button in GUI, it should be run 5 times backend class with different arguments. I am open for any advise.

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP