|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +from __future__ import print_function |
| 4 | + |
| 5 | +from multiprocessing import Process, cpu_count |
| 6 | +import argparse |
| 7 | +import logging |
| 8 | +import os |
| 9 | + |
| 10 | + |
| 11 | +logging.basicConfig(format='[Ant-Thony] %(levelname)s: %(message)s', level=logging.DEBUG) |
| 12 | + |
| 13 | + |
| 14 | +class Task(object): |
| 15 | + """A task class""" |
| 16 | + def __init__(self, command, path="."): |
| 17 | + self.command = command |
| 18 | + self.path = path |
| 19 | + |
| 20 | + def run(self): |
| 21 | + """Runs a command in the given path""" |
| 22 | + os.chdir(self.path) |
| 23 | + os.system(self.command) |
| 24 | + |
| 25 | + |
| 26 | +class Ant(Process): |
| 27 | + """Ant-Thony's buddies""" |
| 28 | + def __init__(self, tasks): |
| 29 | + super(Ant, self).__init__() |
| 30 | + self.tasks = tasks |
| 31 | + logging.info("{} ready with {} tasks".format(self.name, len(self.tasks))) |
| 32 | + |
| 33 | + def run(self): |
| 34 | + """Runs all the assigned tasks""" |
| 35 | + for task in self.tasks: |
| 36 | + task.run() |
| 37 | + logging.info("{} going back to the nest".format(self.name)) |
| 38 | + |
| 39 | + |
| 40 | +class Ant_Thony(object): |
| 41 | + """Our buddy Ant-Thony""" |
| 42 | + def __init__(self, tasks, num_cpus=0): |
| 43 | + try: |
| 44 | + self.num_processes = int(num_cpus) |
| 45 | + if self.num_processes < 1: |
| 46 | + raise ValueError() |
| 47 | + except (ValueError, TypeError): |
| 48 | + logging.warning("Number of cores has not been specified or it is incorrect. Using all available cores.") |
| 49 | + self.num_processes = cpu_count() |
| 50 | + |
| 51 | + logging.info("Ant-Thony will use {} cores".format(self.num_processes)) |
| 52 | + |
| 53 | + self.tasks = tasks |
| 54 | + self.num_tasks = len(tasks) |
| 55 | + self.workers = [] |
| 56 | + workers_tasks = [tasks[i::self.num_processes] for i in xrange(self.num_processes)] |
| 57 | + |
| 58 | + for i in range(self.num_processes): |
| 59 | + worker = Ant(workers_tasks[i]) |
| 60 | + self.workers.append(worker) |
| 61 | + |
| 62 | + def release(self): |
| 63 | + logging.info("Swarming!") |
| 64 | + for ant in self.workers: |
| 65 | + ant.start() |
| 66 | + |
| 67 | + for ant in self.workers: |
| 68 | + ant.join() |
| 69 | + |
| 70 | + logging.info("{} tasks done".format(self.num_tasks)) |
| 71 | + |
| 72 | + def go_home(self): |
| 73 | + for ant in self.workers: |
| 74 | + ant.terminate() |
| 75 | + logging.info("All ants back to the nest") |
| 76 | + |
| 77 | + |
| 78 | +if __name__ == "__main__": |
| 79 | + |
| 80 | + parser = argparse.ArgumentParser(prog='ant_thony') |
| 81 | + parser.add_argument("tasks_file_name", help="A file containing a task for each line", metavar="tasks_file_name") |
| 82 | + parser.add_argument("--cores", "-cores", "-c", help="CPU cores to use", dest="cores", type=int, default=0) |
| 83 | + |
| 84 | + args = parser.parse_args() |
| 85 | + |
| 86 | + with open(args.tasks_file_name) as handle: |
| 87 | + tasks = [] |
| 88 | + for line in handle: |
| 89 | + if line and not line.startswith("#"): |
| 90 | + tasks.append(Task(line.rstrip(os.linesep))) |
| 91 | + |
| 92 | + anthony = Ant_Thony(tasks, args.cores) |
| 93 | + anthony.release() |
| 94 | + anthony.go_home() |
| 95 | + |
0 commit comments