Derek Beckett | db73511 | 2020-08-27 10:25:15 -0700 | [diff] [blame] | 1 | # Lint as: python2, python3 |
| 2 | from __future__ import absolute_import |
| 3 | from __future__ import division |
| 4 | from __future__ import print_function |
mbligh | bd1eb20 | 2008-07-29 22:09:29 +0000 | [diff] [blame] | 5 | import os, sys |
Derek Beckett | db73511 | 2020-08-27 10:25:15 -0700 | [diff] [blame] | 6 | import six |
| 7 | from six.moves import range |
mbligh | bd1eb20 | 2008-07-29 22:09:29 +0000 | [diff] [blame] | 8 | |
| 9 | |
| 10 | class ParallelError(Exception): |
| 11 | def __init__(self, str, errors): |
| 12 | self.str = str |
| 13 | self.errors = errors |
| 14 | Exception.__init__(self, str) |
| 15 | |
| 16 | |
| 17 | class ParallelExecute(object): |
| 18 | def __init__(self, functions, max_simultaneous_procs=20): |
| 19 | """\ |
| 20 | This takes in a dictionary of functions which map to a set of |
| 21 | functions that they depend on. |
| 22 | |
| 23 | functions: This is either a list of or dictionary of functions to |
| 24 | be run. If it's a dictionary, the value should be a set |
| 25 | of other functions this function is dependent on. If its |
| 26 | a list (or tuple or anything iterable that returns a |
| 27 | single element each iteration), then it's assumed that |
| 28 | there are no dependencies. |
| 29 | |
| 30 | max_simultaneous_procs: Throttle the number of processes we have |
| 31 | running at once. |
| 32 | """ |
| 33 | if not isinstance(functions, dict): |
| 34 | function_list = functions |
| 35 | functions = {} |
| 36 | for fn in function_list: |
| 37 | functions[fn] = set() |
| 38 | |
| 39 | dependents = {} |
Derek Beckett | db73511 | 2020-08-27 10:25:15 -0700 | [diff] [blame] | 40 | for fn, deps in six.iteritems(functions): |
mbligh | bd1eb20 | 2008-07-29 22:09:29 +0000 | [diff] [blame] | 41 | dependents[fn] = [] |
Derek Beckett | db73511 | 2020-08-27 10:25:15 -0700 | [diff] [blame] | 42 | for fn, deps in six.iteritems(functions): |
mbligh | bd1eb20 | 2008-07-29 22:09:29 +0000 | [diff] [blame] | 43 | for dep in deps: |
| 44 | dependents[dep].append(fn) |
| 45 | |
| 46 | self.max_procs = max_simultaneous_procs |
| 47 | self.functions = functions |
| 48 | self.dependents = dependents |
| 49 | self.pid_map = {} |
| 50 | self.ready_to_run = [] |
| 51 | |
| 52 | |
| 53 | def _run(self, function): |
| 54 | self.functions.pop(function) |
| 55 | pid = os.fork() |
| 56 | if pid: |
| 57 | self.pid_map[pid] = function |
| 58 | else: |
| 59 | function() |
| 60 | sys.exit(0) |
| 61 | |
| 62 | |
| 63 | def run_until_completion(self): |
Derek Beckett | db73511 | 2020-08-27 10:25:15 -0700 | [diff] [blame] | 64 | for fn, deps in six.iteritems(self.functions): |
mbligh | bd1eb20 | 2008-07-29 22:09:29 +0000 | [diff] [blame] | 65 | if len(deps) == 0: |
| 66 | self.ready_to_run.append(fn) |
| 67 | |
| 68 | errors = [] |
| 69 | while len(self.pid_map) > 0 or len(self.ready_to_run) > 0: |
| 70 | max_allowed = self.max_procs - len(self.pid_map) |
| 71 | max_able = len(self.ready_to_run) |
Derek Beckett | db73511 | 2020-08-27 10:25:15 -0700 | [diff] [blame] | 72 | for i in range(min(max_allowed, max_able)): |
mbligh | bd1eb20 | 2008-07-29 22:09:29 +0000 | [diff] [blame] | 73 | self._run(self.ready_to_run.pop()) |
| 74 | |
| 75 | # Handle one proc that's finished. |
| 76 | pid, status = os.wait() |
| 77 | fn = self.pid_map.pop(pid) |
| 78 | if status != 0: |
| 79 | errors.append("%s failed" % fn.__name__) |
| 80 | continue |
| 81 | |
| 82 | for dependent in self.dependents[fn]: |
| 83 | self.functions[dependent].remove(fn) |
| 84 | if len(self.functions[dependent]) == 0: |
| 85 | self.ready_to_run.append(dependent) |
| 86 | |
mbligh | ab0b038 | 2008-08-21 20:18:04 +0000 | [diff] [blame] | 87 | if len(self.functions) > 0 and len(errors) == 0: |
mbligh | bd1eb20 | 2008-07-29 22:09:29 +0000 | [diff] [blame] | 88 | errors.append("Deadlock detected") |
| 89 | |
| 90 | if len(errors) > 0: |
| 91 | msg = "Errors occurred during execution:" |
| 92 | msg = '\n'.join([msg] + errors) |
| 93 | raise ParallelError(msg, errors) |
| 94 | |
| 95 | |
| 96 | def redirect_io(log_file='/dev/null'): |
| 97 | # Always redirect stdin. |
| 98 | in_fd = os.open('/dev/null', os.O_RDONLY) |
| 99 | try: |
| 100 | os.dup2(in_fd, 0) |
| 101 | finally: |
| 102 | os.close(in_fd) |
| 103 | |
| 104 | out_fd = os.open(log_file, os.O_WRONLY | os.O_CREAT) |
| 105 | try: |
| 106 | os.dup2(out_fd, 2) |
| 107 | os.dup2(out_fd, 1) |
| 108 | finally: |
| 109 | os.close(out_fd) |
| 110 | |
| 111 | sys.stdin = os.fdopen(0, 'r') |
| 112 | sys.stdout = os.fdopen(1, 'w') |
| 113 | sys.stderr = os.fdopen(2, 'w') |