How can I “hook into” Python from C++ when it executes a function? My goal is to profile

独自空忆成欢 提交于 2019-12-24 05:23:06

问题


I'm implementing a profiler in an application and I'm a little flummoxed about how to implement Python profiling such that the results can be displayed in my existing tool. The application allows for Python scripting via communication with the python interpreter.

I was wondering if anyone has ideas on how to profile Python functions from C++

Thank you for your suggestions :)


回答1:


Well not sure what your goal is, but I'd personally not start from zero but use the existing modules for profiling. The cprofile module was written in C and should be easily extensible if the documentation is to be believed: http://docs.python.org/release/3.2/library/profile.html#extensions-deriving-better-profilers




回答2:


The usual technique for profiling already-existing functions that we use in Lua a lot is to overwrite the function with your own version that will start timing, call the original function, and stop timing, returning the value that the original function returned.



来源:https://stackoverflow.com/questions/7033612/how-can-i-hook-into-python-from-c-when-it-executes-a-function-my-goal-is-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!