Mic*_*man 5 python profiling cython python-3.x
我的Python 3项目正在使用cython.
在生产部署中,我使用的是构建脚本,除其他外,它禁用了分析:
from distutils.core import setup
from Cython.Build import cythonize
import os
compiler_directives = {
'language_level': 3,
'optimize.use_switch': True,
'profile': True,
}
setup(
packages=["XXXXXX"],
ext_modules=cythonize(
module_list="**/*.pyx",
compiler_directives=compiler_directives,
)
)
Run Code Online (Sandbox Code Playgroud)
在开发中,我正在使用pyximport.为了区分这两个上下文,我正在测试"生产"用户是否在项目的顶级__init__.py文件中使用.如果这不是生产,我正在使用pyximport; pyximport.install,以便它变得完全透明:
if getpass.getuser != PRODUCTION_USER_NAME:
import pyximport
pyximport.install(
pyximport=True,
pyimport=False,
build_dir=None,
build_in_temp=True,
setup_args={},
reload_support=False,
load_py_module_on_import_failure=False,
inplace=False,
language_level=3,
)
Run Code Online (Sandbox Code Playgroud)
我想在开发环境中为所有cython文件启用分析.我试图将该profile=True参数添加到piximport.install语句中,但它不起作用.
我该怎么办?
一些额外的评论:
我想避免Profile=True在开发过程中推送所有源代码并在提交之前删除它们...
使用.pyxbld文件对我来说不是一个选项,因为我有46个pyx文件,并计划有更多...除非有一种方法只设置一个文件来支持所有pyx,就像我为构建脚本所做的那样,但我没有发现如何.
谢谢你的帮助.
它需要包装 pyximport 的内部函数之一,但可以这样做:
# Allow .pyx files to be seamlessly integrated via cython/pyximport with
# default compiler directives.
import functools
import pyximport.pyximport
# Hack pyximport to have default options for profiling and embedding signatures
# in docstrings.
# Anytime pyximport needs to build a file, it ends up calling
# pyximport.pyximport.get_distutils_extension. This function returns an object
# which has a cython_directives attribute that may be set to a dictionary of
# compiler directives for cython.
_old_get_distutils_extension = pyximport.pyximport.get_distutils_extension
@functools.wraps(_old_get_distutils_extension)
def _get_distutils_extension_new(*args, **kwargs):
extension_mod, setup_args = _old_get_distutils_extension(*args, **kwargs)
if not hasattr(extension_mod, 'cython_directives'):
extension_mod.cython_directives = {}
extension_mod.cython_directives.setdefault('embedsignature', True)
extension_mod.cython_directives.setdefault('profile', True)
return extension_mod, setup_args
pyximport.pyximport.get_distutils_extension = _get_distutils_extension_new
pyximport.install()
Run Code Online (Sandbox Code Playgroud)
请注意,这不会使用新选项强制重新编译未更改的模块;您将必须使用touch这些文件来触发使用新配置的编译。
| 归档时间: |
|
| 查看次数: |
644 次 |
| 最近记录: |