Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright 2015 the V8 project authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """ |
| 7 | This script calls the first argument for each of the following arguments in |
| 8 | parallel. E.g. |
| 9 | parallel.py "clang --opt" file1 file2 |
| 10 | calls |
| 11 | clang --opt file1 |
| 12 | clang --opt file2 |
| 13 | |
| 14 | The output (stdout and stderr) is concatenated sequentially in the form: |
| 15 | ______________ file1 |
| 16 | <output of clang --opt file1> |
| 17 | ______________ finish <exit code of clang --opt file1> ______________ |
| 18 | ______________ file2 |
| 19 | <output of clang --opt file2> |
| 20 | ______________ finish <exit code of clang --opt file2> ______________ |
| 21 | """ |
| 22 | |
| 23 | import itertools |
| 24 | import multiprocessing |
| 25 | import subprocess |
| 26 | import sys |
| 27 | |
| 28 | def invoke(cmdline): |
| 29 | try: |
| 30 | return (subprocess.check_output( |
| 31 | cmdline, shell=True, stderr=subprocess.STDOUT), 0) |
| 32 | except subprocess.CalledProcessError as e: |
| 33 | return (e.output, e.returncode) |
| 34 | |
| 35 | if __name__ == '__main__': |
| 36 | assert len(sys.argv) > 2 |
| 37 | processes = multiprocessing.cpu_count() |
| 38 | pool = multiprocessing.Pool(processes=processes) |
| 39 | cmdlines = ["%s %s" % (sys.argv[1], filename) for filename in sys.argv[2:]] |
| 40 | for filename, result in itertools.izip( |
| 41 | sys.argv[2:], pool.imap(invoke, cmdlines)): |
| 42 | print "______________ %s" % filename |
| 43 | print result[0] |
| 44 | print "______________ finish %d ______________" % result[1] |