Форум сайта python.su
Здравствуйте. Вот такая интересная задачка, над которой уже второй день сижу (Python 2.7.13):
есть класс, который имеет много методов для обработки какой-то коллекции данных, схематически вот:
class DataProcessor(object): def process1(self, data): print "process 1 finished" return data def process2(self, data): print "process 2 finished" return data def process3(self, data): print "process 3 finished" return data def process4(self, data): print "process 4 finished" return data def make_all_processes(self, data): processes = [getattr(self, method) for method in dir(self) if method.startswith('process')] results = [process(data) for process in processes] return results
results = multiprocessing.Manager().dict() processes = [] for lookup in lookup_methods: process = multiprocessing.Process(target=self._lookup_process_wrapper, args=(lookup, input_data, results)) process.start() processes.append(process) for process in processes: process.join()
def lookup_process_wrapper(lookups_queue, data, results, config_dict): while True: try: lookup = lookups_queue.pop() except: break data_processor = DataProcessor(config_dict) lookup_method = getattr(data_processor, lookup) results[lookup] = lookup_method(data=data)
Отредактировано Master_Sergius (Авг. 4, 2017 13:52:13)
Офлайн