Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8765763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:22:12+00:00 2026-06-13T16:22:12+00:00

I am programming a C++ extension for Python and I am using distutils to

  • 0

I am programming a C++ extension for Python and I am using distutils to compile the project. As the project grows, rebuilding it takes longer and longer. Is there a way to speed up the build process?

I read that parallel builds (as with make -j) are not possible with distutils. Are there any good alternatives to distutils which might be faster?

I also noticed that it’s recompiling all object files every time I call python setup.py build, even when I only changed one source file. Should this be the case or might I be doing something wrong here?

In case it helps, here are some of the files which I try to compile: https://gist.github.com/2923577

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T16:22:13+00:00Added an answer on June 13, 2026 at 4:22 pm
    1. Try building with environment variable CC="ccache gcc", that will speed up build significantly when the source has not changed. (strangely, distutils uses CC also for c++ source files). Install the ccache package, of course.

    2. Since you have a single extension which is assembled from multiple compiled object files, you can monkey-patch distutils to compile those in parallel (they are independent) – put this into your setup.py (adjust the N=2 as you wish):

      # 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 code
          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. For the sake of completeness, if you have multiple extensions, you can use the following solution:

      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 releases
          class 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
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently programming an extension to a program, which only supports i386 (and I
Programming has come a long way. I am still relatively young (first Computer: C64),
im programming in c++ using ado, the documnet on tsql says the OUTPUT clause
e4x programming extension is native to Actionscript and makes sense to use it over
I'm writing (well, completing) an extension of Java which will help role programming. I
I'm in the process of working on programming project that involves some pretty extensive
When programming againt virual machine, some system remind hardware virtualization extension is needed. What
Are there advantages or disadvantages to the file extension used for SQLite databases? It
I started programming in Python a couple of days ago and I have a
I'm searching for some extension methods that helps programming in ASP.NET. Some functions to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.