I'm new to both Kivy and multiprocessing.
I'm exploring running multiple Kivy Apps at the same time, using multiprocessing, in the same way as in: Running multiple Kivy apps at same time that communicate with each other.
Now, I want to maintain a shared list of the Apps currently running, for example with Manager.list()
but I got stuck at the start. I've got a test code like that:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from multiprocessing import Process, Manager
class ParentApp(App):
def __init__(self, app_list, **kwargs):
super(ParentApp, self).__init__(**kwargs)
self.app_list = app_list
def build(self):
self.add_self_to_list()
return Label(text = "abc")
# def on_start(self):
# self.add_self_to_list()
def add_self_to_list(self):
self.app_list.append(self)
if __name__ == "__main__":
manager = Manager()
my_list = manager.list()
print "Before:"
print my_list
parent = ParentApp(my_list)
p = Process(target = parent.run)
p.start()
p.join()
print "After:"
print my_list
This adds the ParentApp
to the shared my_list
correctly, but when I comment out the self.add_self_to_list()
in build
, and uncomment on_start
, I get this PicklingError:
...
self.app_list.append(self)
File "<string>", line 2, in append
File "C:\Kivy-1.9.0-py2.7-win32-x64\Python27\lib\multiprocessing\managers.py", line 758, in _callmethod
conn.send((self._id, methodname, args, kwds))
PicklingError: Can't pickle <type 'code'>: attribute lookup __builtin__.code failed
I've also tried hooking add_self_to_list
to the on_press
callback of a Button, and I got the same error. Why does it work on the build
method and not on callbacks? Is there any way around this?
I'm running Kivy 1.9.0 and Python 2.7 on Windows 7.
(In case you're wondering why I'm trying this way: each App can potentially call other Apps. If an App is already running, I don't want to start a new one and pollute the screen with copies of the same window. Ideally I would get hold of the App and pop its window in the front. I haven't had a go with Kivy App's root_window yet, so I'm not even sure if that is possible. Just looking into the options here :) If you know already if this can or can't be done with Kivy, I'd also like to know, but maybe I'll post another question! )