def run(self):
    task_master = self.run_control._task_master
    status = MPI.Status()
    if (self.size > len(task_master.pending_tasks)):
        logger.warning('MPI size > # of pending tasks, not sure what will happen')
    waiting_dests = list(range(1, self.size)[::(- 1)])
    while True:
        try:
            task = task_master.get_next_pending()
            if (not task):
                waiting_dests.append(dest)
                logger.debug('appended waiting dests: {}', waiting_dests)
        except StopIteration:
            logger.debug('All tasks sent')
            break
        need_to_block = ((not waiting_dests) or (not task))
        if need_to_block:
            rdata = self.comm.recv(source=MPI.ANY_SOURCE, tag=MPI.ANY_TAG, status=status)
            logger.info('Data received from {}', status.Get_source())
            logger.debug('data: {}', rdata)
            if (rdata['command'] == 'error'):
                logger.error('Rank {} raised error', status.Get_source())
                logger.error(rdata['msg'])
                raise Exception('Unrecoverable error')
            received_task = rdata['task']
            task_master.update_task(received_task.index, received_task.status)
        if task:
            if waiting_dests:
                logger.debug('pop waiting dests: {}', waiting_dests)
                dest = waiting_dests.pop()
            else:
                dest = status.Get_source()
            data = {'command': 'run_task', 'task': task}
            logger.info('Sending data to {}', dest)
            logger.debug('data: {}', data)
            self.comm.send(data, dest=dest, tag=WORKTAG)
    for dest in range(1, (self.size - len(waiting_dests))):
        rdata = self.comm.recv(source=MPI.ANY_SOURCE, tag=MPI.ANY_TAG, status=status)
        if (rdata['command'] == 'error'):
            logger.error('Rank {} raised error', status.Get_source())
            logger.error(rdata['msg'])
            raise Exception('Unrecoverable error')
        received_task = rdata['task']
        task_master.update_task(received_task.index, received_task.status)
        logger.info('Final data received from {}', status.Get_source())
        logger.debug('data: {}', rdata)
    for dest in range(1, self.size):
        data = {'command': 'die'}
        logger.info('Sending die to {}', dest)
        self.comm.send(data, dest=dest, tag=DIETAG)
    logger.info('Finished')
def run(self):
    task_master = self.run_control._task_master
    status = MPI.Status()
    if (self.size > len(task_master.pending_tasks)):
        logger.warning('MPI size > # of pending tasks, not sure what will happen')
    waiting_dests = list(range(1, self.size)[::(- 1)])
    while True:
        try:
            task = task_master.get_next_pending()
            if (not task):
                waiting_dests.append(dest)
                logger.debug('appended waiting dests: {}', waiting_dests)
        except StopIteration:
            logger.debug('All tasks sent')
            break
        need_to_block = ((not waiting_dests) or (not task))
        if need_to_block:
            rdata = self.comm.recv(source=MPI.ANY_SOURCE, tag=MPI.ANY_TAG, status=status)
            logger.info('Data received from {}', status.Get_source())
            logger.debug('data: {}', rdata)
            if (rdata['command'] == 'error'):
                logger.error('Rank {} raised error', status.Get_source())
                logger.error(rdata['msg'])
                raise Exception('Unrecoverable error')
            received_task = rdata['task']
            task_master.update_task(received_task.index, received_task.status)
        if task:
            if waiting_dests:
                logger.debug('pop waiting dests: {}', waiting_dests)
                dest = waiting_dests.pop()
            else:
                dest = status.Get_source()
            data = {'command': 'run_task', 'task': task}
            logger.info('Sending data to {}', dest)
            logger.debug('data: {}', data)
            self.comm.send(data, dest=dest, tag=WORKTAG)
    for dest in range(1, (self.size - len(waiting_dests))):
        rdata = self.comm.recv(source=MPI.ANY_SOURCE, tag=MPI.ANY_TAG, status=status)
        if (rdata['command'] == 'error'):
            logger.error('Rank {} raised error', status.Get_source())
            logger.error(rdata['msg'])
            raise Exception('Unrecoverable error')
        received_task = rdata['task']
        task_master.update_task(received_task.index, received_task.status)
        logger.info('Final data received from {}', status.Get_source())
        logger.debug('data: {}', rdata)
    for dest in range(1, self.size):
        data = {'command': 'die'}
        logger.info('Sending die to {}', dest)
        self.comm.send(data, dest=dest, tag=DIETAG)
    logger.info('Finished')