0

All, In my GUI I'm using multiprocessing to run a function. But the pool start multiple GUI. I have read that peoples add if __name__ == '__main__': in their code and it seems to work. But I don't know if this trick will work in my case, and where I have to insert this code. The function run_func() is launched by a button in the GUI. How can I block this multiple start?

I have a second question: How can I do to unimport setup at the end of the exec?

Thanks a lot !

@pyqtSlot()
    def run_func():
        run="""
        import os
        import sys
        from setup import *
        print('toto')
        print('titi')
        """
        from multiprocessing import Pool   
        pool = Pool(processes=4)          
        asyncResult = pool.apply_async(exec(run),{},{}),range(1)
dano
  • 91,354
  • 19
  • 222
  • 219
  • Is your GUI launched in setup.py? Is launching it not protected by `if __name__ == "__main__":`? Also, why don't you just create a regular function and call `apply_async` on that? Using `exec` the way you are is not the right way do to it. – dano Jun 05 '14 at 16:44
  • Thank you dano. My GUI is not launched in setup.py. I have a Main which launch a QApplication which launch my QMainWindow. Then my QMainWindow launch run_func() by an QAction.I can directly lauch a function in apply_async you are right but it don't solve the multiple start. – user3393374 Jun 06 '14 at 07:38

1 Answers1

0

You don't provide much context for your question. Anyway I made a test removing the from setup import * part from the run string. And all run well, hence, is not a PyQT problem, is more like at some point you're executing again the module/function that runs the GUI.

For the first question:

I recommend you to use a debugger and set some breakpoints where the GUI is launched, then you can use the call-stack for figureout who is calling your GUI. That way yuo will know where the 'main' code block goes, and even if it is really necessary.

As debugger use pdb with in-code breakpoints (remember you're running in multiprocess) put the line:

import pdb; pdb.set_trace()

where ever yuo want to set a breakpoint.

For the second question:

See, How do I unload (reload) a Python module?

Community
  • 1
  • 1
Raydel Miranda
  • 13,825
  • 3
  • 38
  • 60
  • Thanks for your answer. I find a lot of problem and I think multiprocessing is not what i am looking for. I am looking for a way to run a script outside of my GUI. I want to run a script in a container or something like that. – user3393374 Jun 12 '14 at 08:22
  • Just post another question, I´m sure you will find help here. – Raydel Miranda Jun 12 '14 at 13:15
  • Thank you, I posted it.[link](http://stackoverflow.com/questions/24180766/run-python-script-in-a-container-in-a-gui?noredirect=1#comment37336844_24180766) – user3393374 Jun 12 '14 at 15:09