K
K
Kakrik2021-08-10 14:28:43
Python
Kakrik, 2021-08-10 14:28:43

Mystery of Python GUI (Kivy) and Multiprocessing?

Here is my simple code that, when the button is clicked, should run the run_bot() function :

from kivy.app import App
from kivy.uix.button import Button
import multiprocessing as mp
import time
def run_bot():
    while True:
        print('Working...')
        time.sleep(2)
    
class MyApp(App):
        def build(self):
            self.btn=Button(
                        text='Старт',
                        font_size=30,
                        on_press=self.start,
                        background_color=(1,3,3,10)
                        )
            return self.btn
        def start(self,instance):
            self.proc=mp.Process(target=run_bot)
            self.proc.daemon=True
            self.proc.start() 
            
if __name__ == '__main__':
    MyApp().run()


When I double-click this .py file , the terminal, GUI opens, and the code runs flawlessly. (I work through the usual IDLE 3.9)

However, if the Python file is compiled into an .exe using the same pyinstaller, when I press a button in my application, the program overlays another one on top of the main application window , and this way you can create at least 200 identical windows. However, the run_bot() script does not run. ( I compile with the terminal, of course.)

For convenience, I recorded it on *video* .

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-08-10
@Kakrik

In pyinstaller (and other "compilers") for multiprocessor applications, you need to do it mp.freeze_support()right after if __name__ == '__main__':.
But such actions are still entertainment, in the future something else will fall off
https://docs.python.org/3/library/multiprocessing....

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question