栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用distutils加快构建过程

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用distutils加快构建过程

  1. 尝试使用环境变量

    CC="ccache gcc"
    构建,这将在源未更改时显着加快构建速度。(奇怪的是,distutils
    CC
    也用于c ++源文件)。当然,请安装ccache软件包。

  2. 由于您有一个 扩展名 ,它是由 多个编译的目标文件组成的 ,因此可以用猴子补丁distutils并行编译它们(它们是独立的)-将其放入setup.py(

    N=2
    根据需要进行调整):

    # monkey-patch for parallel compilation

    def parallelCCompile(self, sources, output_dir=None, macros=None, include_dirs=None, debug=0, extra_preargs=None, extra_postargs=None, depends=None):
    # those lines are copied from distutils.ccompiler.CCompiler directly
    macros, objects, extra_postargs, pp_opts, build = self._setup_compile(output_dir, macros, include_dirs, sources, depends, extra_postargs)
    cc_args = self._get_cc_args(pp_opts, debug, extra_preargs)
    # parallel pre
    N=2 # number of parallel compilations
    import multiprocessing.pool
    def _single_compile(obj):
    try: src, ext = build[obj]
    except KeyError: return
    self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts)
    # convert to list, imap is evaluated on-demand
    list(multiprocessing.pool.ThreadPool(N).imap(_single_compile,objects))
    return objects
    import distutils.ccompiler
    distutils.ccompiler.CCompiler.compile=parallelCCompile

  3. 为了完整起见,如果您具有 多个扩展名 ,则可以使用以下解决方案:

    import os

    import multiprocessing
    try:
    from concurrent.futures import ThreadPoolExecutor as Pool
    except importError:
    from multiprocessing.pool import ThreadPool as LegacyPool

    # To ensure the with statement works. Required for some older 2.7.x releasesclass Pool(LegacyPool):    def __enter__(self):        return self    def __exit__(self, *args):        self.close()        self.join()

    def build_extensions(self):
    “”“Function to monkey-patch
    distutils.command.build_ext.build_ext.build_extensions

    """self.check_extensions_list(self.extensions)try:    num_jobs = os.cpu_count()except AttributeError:    num_jobs = multiprocessing.cpu_count()with Pool(num_jobs) as pool:    pool.map(self.build_extension, self.extensions)

    def compile(
    self, sources, output_dir=None, macros=None, include_dirs=None,
    debug=0, extra_preargs=None, extra_postargs=None, depends=None,
    ):
    “”“Function to monkey-patch distutils.ccompiler.CCompiler”“”
    macros, objects, extra_postargs, pp_opts, build = self._setup_compile(
    output_dir, macros, include_dirs, sources, depends, extra_postargs
    )
    cc_args = self._get_cc_args(pp_opts, debug, extra_preargs)

    for obj in objects:    try:        src, ext = build[obj]    except KeyError:        continue    self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts)# Return *all* object filenames, not just the ones we just built.return objects

    from distutils.ccompiler import CCompiler
    from distutils.command.build_ext import build_ext
    build_ext.build_extensions = build_extensions
    CCompiler.compile = compile



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/625041.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号