Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use multiprocessing in flexx after freeze, The window keeps popping up #749

Open
1751660300 opened this issue Apr 17, 2024 · 8 comments
Open

Comments

@1751660300
Copy link

1751660300 commented Apr 17, 2024

Hello Brother.
i want to use multiprocessing in PyWidget.
In pycharm, the test runs normally, but when packaged into an executable file, the window keeps popping up and cannot be closed.

code:

        def download_multi(package_names):
            # multiprocessing task
            .....
       @flx.action
        def d_action():
            ....
            download_multi(package_names) 

This question has been bothering me. HA.

@almarklein
Copy link
Member

I'm sorry but this is not enough information to provide any help. Is it possible to create example code, that is as small as possible, but that I can run and that demonstrates the problem?

@1751660300
Copy link
Author

ok, this is a complete example, you need
You need to package an application through the freeze method, then run, click this button.

import time
from multiprocessing import Pool
from flexx import flx

def down_single(index):
    print(f"{index}")
    time.sleep(index)

def down_multi():
    executor = Pool(2)
    for i in range(4):
        executor.apply_async(down_single, args=(i, ))
    executor.close()
    executor.join()


class Example(flx.PyWidget):

    def init(self):
        with flx.VBox():
            self.lable = flx.MultiLineEdit()
            self.button = flx.Button(text="test")
            flx.HBox(flex=1)

    @flx.reaction("button.pointer_click")
    def down_btn(self, *events):
        self.down_ac()

    @flx.action
    def down_ac(self):
        down_multi()
        self.lable.set_text("success!")

@1751660300
Copy link
Author

After running, multiple windows will be launched and cannot be closed, as if in a computer virus, hahaha

@almarklein
Copy link
Member

Did you put the code to launch the app in an if __name__ == "__main__"?

if __name__ == '__main__':
    m = flx.App(Example).launch('firefox')
    flx.run()

@1751660300
Copy link
Author

Here's the code I packed

def test_make_package_3(self):
flx.App(Example).freeze("./", excludes=[], launch="app")

@almarklein
Copy link
Member

Oh, I have no idea how multiprocessing works together with frozen apps. But I do know that when you "launch the pool", it spawns new processes using the same __main__ module, so if you don't guard with if __name__ == "__main__" it will re-launch the app again, and again ...

@1751660300
Copy link
Author

yeah,i run with if name == "main", the program running ok,hope this problem can be solved later. thank you

@1751660300
Copy link
Author

hello,
Pyinstaller packages programs that use multiprocessing must call the multiprocessing.freeze_support () method to freeze the process. I try to write the generated examp.py in the following format to solve this problem.

pyinstaller.exe "E:\code\flexx\learning\example.spec"

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants