1

I'm trying to develop a kivy app for my less-comp-savvy colleagues which wraps a nice GUI around some computations which I developed for a project; the computation includes using the pathos.multiprocessing.ProcessPool() to distribute the work for a faster run time, but when kivy tries to execute the computation it opens tens of program clones and then freezes. Have tried integrating freeze_support but to no avail. How can I get kivy to execute a ProcessPool request without going haywire? Coded in Python 3.7.2, using the PyCharm IDE.

--GUI.py file--

import kivy
kivy.require("1.9.0")

from kivy.config import Config
Config.set('graphics', 'width', '600')
Config.set('graphics', 'height', '300')

from calc import main
from kivy.app import App
from kivy.uix.gridlayout import GridLayout


class CalcGridLayout(GridLayout):
    def calculate(self):
        main()

class graphics(App):

    def build(self):
        return CalcGridLayout()

graphics().run()

--calc.py file (example error)--

def main():
    import numpy as np
    from pathos.multiprocessing import ProcessPool as Pool

    grid = np.array([(m, n)
                     for m in np.arange(1, 1000, 0.1)
                     for n in np.arange(1, 1000, 0.1)])

    def calc(grid):
        var1 = grid[0]
        var2 = grid[1]
        y = var1*var2
        return y

    res = Pool().map(calc, grid)
    return res

-- graphics.kv file--

<CalcGridLayout>:
    id: calculator
    rows: 7
    padding: 10
    spacing: 10

    # Where input is displayed

    BoxLayout:
        height: 10
        Label:
            spacing: 10
            text: '#MoProblems'

    BoxLayout:

        Button:
            id: run_button
            text: "Run"
            on_press: root.calculate()
            on_press: self.background_color = (1, 1, 0, 1)

Pressing the 'run' button in the app tries to execute the code, but it starts cloning the app on screen and then freezing

Michael Green
  • 719
  • 6
  • 15
  • 1
    Not sure if this is your problem, but anytime you are using multiprocessing, you must protect your app.run() call with `if __name__ == "__main__":`. – John Anderson Jun 21 '19 at 01:57
  • Worked perfectly. To note, I had tried adding that conditional previously, but it seems that I was placing such statement in the wrong spots. Binding the app.run() command was the answer [to be explicit for anyone's future reference, for the example herein, it's the graphics().run() line in GUI.py requires the conditional]. Thanks John. – Michael Green Jun 21 '19 at 13:12

0 Answers0