|
7 | 7 |
|
8 | 8 | from __future__ import print_function
|
9 | 9 |
|
| 10 | +import io |
| 11 | +import os |
10 | 12 | import re
|
11 | 13 | import subprocess
|
12 | 14 | import sys
|
13 |
| -import io |
14 |
| -import os |
| 15 | +import multiprocessing, multiprocessing.dummy |
15 | 16 |
|
16 | 17 |
|
17 | 18 | # Extract MP_QSTR_FOO macros.
|
@@ -39,11 +40,27 @@ def preprocess():
|
39 | 40 | os.makedirs(os.path.dirname(args.output[0]))
|
40 | 41 | except OSError:
|
41 | 42 | pass
|
42 |
| - with open(args.output[0], "w") as out_file: |
43 |
| - if csources: |
44 |
| - subprocess.check_call(args.pp + args.cflags + csources, stdout=out_file) |
45 |
| - if cxxsources: |
46 |
| - subprocess.check_call(args.pp + args.cxxflags + cxxsources, stdout=out_file) |
| 43 | + |
| 44 | + def pp(flags): |
| 45 | + def run(files): |
| 46 | + return subprocess.check_output(args.pp + flags + files) |
| 47 | + |
| 48 | + return run |
| 49 | + |
| 50 | + try: |
| 51 | + cpus = multiprocessing.cpu_count() |
| 52 | + except NotImplementedError: |
| 53 | + cpus = 1 |
| 54 | + p = multiprocessing.dummy.Pool(cpus) |
| 55 | + with open(args.output[0], "wb") as out_file: |
| 56 | + for flags, sources in ( |
| 57 | + (args.cflags, csources), |
| 58 | + (args.cxxflags, cxxsources), |
| 59 | + ): |
| 60 | + batch_size = (len(sources) + cpus - 1) // cpus |
| 61 | + chunks = [sources[i : i + batch_size] for i in range(0, len(sources), batch_size or 1)] |
| 62 | + for output in p.imap(pp(flags), chunks): |
| 63 | + out_file.write(output) |
47 | 64 |
|
48 | 65 |
|
49 | 66 | def write_out(fname, output):
|
|
0 commit comments