Skip to content

Commit

Permalink
Merge pull request #791 from wiredfool/multiprocessing_fail
Browse files Browse the repository at this point in the history
Disable mp_compile when Multiprocess.Pool fails
  • Loading branch information
aclark4life committed Jul 9, 2014
2 parents 33e8480 + 919315e commit 878a4e1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ pre:
clean:
python setup.py clean
rm PIL/*.so || true
find . -name __pycache__ | xargs rm -r
rm -r build || true
find . -name __pycache__ | xargs rm -r || true


install:
python setup.py install
Expand Down
28 changes: 19 additions & 9 deletions mp_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
from distutils.ccompiler import CCompiler
import os

try:
MAX_PROCS = int(os.environ.get('MAX_CONCURRENCY', cpu_count()))
except:
MAX_PROCS = None


# hideous monkeypatching. but. but. but.
def _mp_compile_one(tp):
Expand All @@ -31,22 +36,27 @@ def _mp_compile(self, sources, output_dir=None, macros=None,
output_dir, macros, include_dirs, sources, depends, extra_postargs)
cc_args = self._get_cc_args(pp_opts, debug, extra_preargs)

try:
max_procs = int(os.environ.get('MAX_CONCURRENCY', cpu_count()))
except:
max_procs = None
pool = Pool(max_procs)
pool = Pool(MAX_PROCS)
try:
print ("Building using %d processes" % pool._processes)
except:
pass
arr = [
(self, obj, build, cc_args, extra_postargs, pp_opts) for obj in objects
]
arr = [(self, obj, build, cc_args, extra_postargs, pp_opts)
for obj in objects]
pool.map_async(_mp_compile_one, arr)
pool.close()
pool.join()
# Return *all* object filenames, not just the ones we just built.
return objects

CCompiler.compile = _mp_compile
# explicitly don't enable if environment says 1 processor
if MAX_PROCS != 1:
try:
# bug, only enable if we can make a Pool. see issue #790 and
# http://stackoverflow.com/questions/6033599/oserror-38-errno-38-with-multiprocessing
pool = Pool(2)
CCompiler.compile = _mp_compile
except Exception as msg:
print("Exception installing mp_compile, proceeding without: %s" %msg)
else:
print("Single threaded build, not installing mp_compile: %s processes" %MAX_PROCS)

0 comments on commit 878a4e1

Please sign in to comment.