如何分析 Kivy 应用程序?

Posted

技术标签:

【中文标题】如何分析 Kivy 应用程序?【英文标题】:How can I profile a Kivy application? 【发布时间】:2016-08-25 22:50:52 【问题描述】:

我正在使用 Kivy 构建游戏。我遇到了性能问题,所以我决定分析该程序。

我尝试通过以下方式运行它:

python -m cProfile main.py

应用程序屏幕保持黑色。几秒钟后,一个异常使程序崩溃:

 Traceback (most recent call last):
   File "c:\python27\Lib\runpy.py", line 162, in _run_module_as_main
     "__main__", fname, loader, pkg_name)
   File "c:\python27\Lib\runpy.py", line 72, in _run_code
     exec code in run_globals
   File "c:\python27\Lib\cProfile.py", line 199, in <module>
     main()
   File "c:\python27\Lib\cProfile.py", line 192, in main
     runctx(code, globs, None, options.outfile, options.sort)
   File "c:\python27\Lib\cProfile.py", line 49, in runctx
     prof = prof.runctx(statement, globals, locals)
   File "c:\python27\Lib\cProfile.py", line 140, in runctx
     exec cmd in globals, locals
   File "main.py", line 26, in <module>
     GameApp().run()
   File "C:\Users\Aviv\Envs\game\lib\site-packages\kivy\app.py", line 828, in run
     runTouchApp()
   File "C:\Users\Aviv\Envs\game\lib\site-packages\kivy\base.py", line 487, in runTouchApp
     EventLoop.window.mainloop()
   File "C:\Users\Aviv\Envs\game\lib\site-packages\kivy\core\window\window_sdl2.py", line 619, in mainloop
     self._mainloop()
   File "C:\Users\Aviv\Envs\game\lib\site-packages\kivy\core\window\window_sdl2.py", line 403, in _mainloop
     x, y = self._fix_mouse_pos(x, y)
   File "C:\Users\Aviv\Envs\game\lib\site-packages\kivy\core\window\window_sdl2.py", line 358, in _fix_mouse_pos
     self.mouse_pos = x, self.system_size[1] - y
   File "kivy\properties.pyx", line 408, in kivy.properties.Property.__set__ (kivy\properties.c:5114)
   File "kivy\properties.pyx", line 446, in kivy.properties.Property.set (kivy\properties.c:5876)
   File "kivy\properties.pyx", line 501, in kivy.properties.Property.dispatch (kivy\properties.c:6557)
   File "kivy\_event.pyx", line 1224, in kivy._event.EventObservers.dispatch (kivy\_event.c:13497)
   File "kivy\_event.pyx", line 1130, in kivy._event.EventObservers._dispatch (kivy\_event.c:12696)
   File "game\opening_screen.py", line 22, in _on_mouse_pos
     if self.ids.start_button.collide_point(*position):
   File "kivy\properties.pyx", line 757, in kivy.properties.ObservableDict.__getattr__ (kivy\properties.c:11882)
 AttributeError: 'super' object has no attribute '__getattr__'

为什么会发生这种情况,如何分析我的 Kivy 应用程序?

【问题讨论】:

【参考方案1】:

kivy 文档指出您无法从命令行执行此操作。

https://kivy.org/docs/api-kivy.app.html

你可以这样做:(从文档复制)

import cProfile

class MyApp(App):
    def on_start(self):
        self.profile = cProfile.Profile()
        self.profile.enable()

    def on_stop(self):
        self.profile.disable()
        self.profile.dump_stats('myapp.profile')

【讨论】:

谢谢老兄 :) 很有帮助。

以上是关于如何分析 Kivy 应用程序?的主要内容,如果未能解决你的问题,请参考以下文章

输入直到离开kivy时如何调用函数?

如何使用来自另一个 kivy 应用程序的参数启动 kivy 应用程序

如何在任务栏中将图标设置为 kivy 应用程序?

如何在 Kivy 中关闭全屏?

如何同时运行 kivy 和烧瓶应用程序?

如何使用 kivy、pyjnius 为 android 制作 GPS 应用程序?