Search code examples
pythonmultiprocessingkivyforkspawn

python- infinite loop in a process causes kivy instances to spawn forverer


I have a kivy app that uses multiprocessing to produce a processes with an infinite loop.

The code works fine on ubuntu -since linux uses forking as a default-. The problem is that in windows OS, spawning is the default so with each iteration, the process go through the code from the beginning, creating an infinite kivy GUI instances.

How can I solve this problem? This is how I start my process:

def f(x):
    while True:
        print(x)

if __name__ == '__main__':
    p = Process(target=f, args=('bob',))
    p.start()

note: I don't use p.join() since the function never ends.


Solution

  • Problem is addressed by this kivy issue log

    Summary

    Issue on windows (not Linux), when multiprocessing.Manager is used, kivy will repeatedly creates a new window

    Solution

    [Add Freeze Support] (https://docs.python.org/2/library/multiprocessing.html#multiprocessing.freeze_support) before starting multiple processing

    Example

    if __name__ == '__main__':
        freeze_support()
        Process(target=f).start()
    

    Addtional Background

    Python multiprocessing is different under Linux and Windows referred by Mohammed Baashar (see comments)