首页 > 解决方案 > Kivy + 多处理引发 TypeError

问题描述

有人能解释一下为什么multiprocessing这么奇怪kivy吗?

最小示例:

from kivy.app import App
from kivy.uix.button import Button
from multiprocessing import Process

class myApp(App):
    def f(self):
        print('test')

    def test(self, caller):
        pr = Process(target=self.f)
        pr.start()

    def build(self):
        btn = Button(text='Go')
        btn.bind(on_press=self.test)

        return btn

if __name__ == '__main__':
    myApp().run()

我收到以下错误:

 Traceback (most recent call last):
   File "/Users/eab06/Desktop/WJB/PythonProjects/Boring Button/test.py", line 20, in <module>
     myApp().run()
   File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/kivy/app.py", line 950, in run
     runTouchApp()
   File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/kivy/base.py", line 582, in runTouchApp
     EventLoop.mainloop()
   File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/kivy/base.py", line 347, in mainloop
     self.idle()
   File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/kivy/base.py", line 391, in idle
     self.dispatch_input()
   File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/kivy/base.py", line 342, in dispatch_input
     post_dispatch_input(*pop(0))
   File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/kivy/base.py", line 248, in post_dispatch_input
     listener.dispatch('on_motion', etype, me)
   File "_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
   File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/kivy/core/window/__init__.py", line 1412, in on_motion
     self.dispatch('on_touch_down', me)
   File "_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
   File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/kivy/core/window/__init__.py", line 1428, in on_touch_down
     if w.dispatch('on_touch_down', touch):
   File "_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
   File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/kivy/uix/behaviors/button.py", line 151, in on_touch_down
     self.dispatch('on_press')
   File "_event.pyx", line 705, in kivy._event.EventDispatcher.dispatch
   File "_event.pyx", line 1248, in kivy._event.EventObservers.dispatch
   File "_event.pyx", line 1172, in kivy._event.EventObservers._dispatch
   File "/Users/eab06/Desktop/WJB/PythonProjects/Boring Button/test.py", line 11, in test
     pr.start()
   File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/process.py", line 121, in start
     self._popen = self._Popen(self)
   File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/context.py", line 224, in _Popen
     return _default_context.get_context().Process._Popen(process_obj)
   File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/context.py", line 283, in _Popen
     return Popen(process_obj)
   File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/popen_spawn_posix.py", line 32, in __init__
     super().__init__(process_obj)
   File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/popen_fork.py", line 19, in __init__
     self._launch(process_obj)
   File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/popen_spawn_posix.py", line 47, in _launch
     reduction.dump(process_obj, fp)
   File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/reduction.py", line 60, in dump
     ForkingPickler(file, protocol).dump(obj)
   File "stringsource", line 2, in kivy._event.EventDispatcher.__reduce_cython__
 TypeError: no default __reduce__ due to non-trivial __cinit__

问题是,我发誓我在一小时前就开始工作了,但我改变了一些东西,忘记了分叉。任何帮助/解释将不胜感激。

标签: python-3.xkivy

解决方案


推荐阅读