Ejemplo n.º 1
0
    def test_update_epoch(self):
        class Task(task.Task):
            called = 0

            def run(self):
                self.called += 1
                yield 0.1
                self.called += 1
                yield 0.1
                self.called += 1

        manager = task.TaskManager(12345.0)
        t = Task()
        manager.add(t)
        assert t.called == 0
        manager.update(12345.0)
        assert t.called == 1
        manager.update(12345.05)
        assert t.called == 1
        manager.update(12345.09)
        assert t.called == 1
        manager.update(12345.11)
        assert t.called == 2
        manager.update(12345.19)
        assert t.called == 2
        manager.update(12345.21)
        assert t.called == 3
        manager.update(12346.0)
        assert t.called == 3
Ejemplo n.º 2
0
    def test_add_remove(self):
        class Task(task.Task):
            called = 0

            def run(self):
                self.called += 1
                yield 0.1
                self.called += 1

        t1 = Task()
        t2 = Task()
        manager = task.TaskManager(0.0)
        t1_added = manager.add(t1)
        t2_added = manager.add(t2)
        assert t1_added is t1
        assert t2_added is t2
        assert t1.called == 0
        assert t2.called == 0
        manager.update(0.0)
        assert t1.called == 1
        assert t2.called == 1
        manager.add(t1)  # this must do nothing
        manager.remove(t2)
        manager.update(0.1)
        assert t1.called == 2
        assert t2.called == 1
        manager.remove(t2)  # this must do nothing
Ejemplo n.º 3
0
    def test_custom_return_value(self):
        def do_task(task):
            yield 0.1
            task.custom_value = 123

        manager = task.TaskManager(0.0)
        t = manager.add(do_task)
        assert not (hasattr(t, 'custom_value'))
        manager.update(0.2)
        assert t.custom_value == 123
Ejemplo n.º 4
0
    def __init__(self, config=ConfigData()):

        tmp = sp.call("clear", shell=True)
        print("TASKMASTER!!")
        self.loop = False
        #	print (config.data)
        self.task_list = task.TaskManager(create_tasks(config.data))
        self.config = config
        for process in config.data:
            if config.data[process]["AUTO_S"] == "true":
                self.start(process)
Ejemplo n.º 5
0
    def test_exception(self):
        def throws_exception(task):
            yield 0.0
            raise Exception()

        manager = task.TaskManager(0.0)
        t = manager.add(throws_exception)
        # The TaskManager should not die, even if the task has thrown an
        # exception.
        manager.update(0.1)
        assert not t.alive
        assert not t.success
        assert t.exception
Ejemplo n.º 6
0
    def test_taskinfo(self):
        def do_task(task):
            assert task.count == 1
            assert task.time < 0.1
            yield 0.1
            assert task.count == 2
            assert task.time >= 0.1 and task.time < 0.11
            last = task.time
            yield 0.1
            assert task.count == 3
            assert task.time >= 0.2 and task.time < 0.21
            assert (task.time - last) - task.dtime < 1.0e-5

        manager = task.TaskManager(12345.0)
        manager.add(do_task)
        run_manager(manager, 0.5)
Ejemplo n.º 7
0
    def test_blocking_task(self):
        def blocking(task):
            yield 3.0

        def blocked(task, manager, param):
            yield 1.0
            param['stage'] = 1
            t = manager.add(blocking)
            param['blocking'] = t
            yield t
            param['stage'] = 2
            yield 1.0

        manager = task.TaskManager(0.0)
        param = {'stage': 0, 'blocking': None}
        t1 = manager.add(blocked, manager, param)
        manager.update(0.0)
        assert t1.running
        assert param['stage'] == 0
        manager.update(0.99)
        assert t1.running
        assert param['stage'] == 0
        assert param['blocking'] is None
        manager.update(1.01)
        assert not t1.running
        assert param['stage'] == 1
        t2 = param['blocking']
        assert t2 is not None
        assert t2.running
        assert t2.count == 1
        manager.update(3.99)
        assert not t1.running
        assert t2.running
        assert param['stage'] == 1
        manager.update(4.01)
        assert t1.running
        assert not t2.running
        assert not t2.alive
        assert param['stage'] == 2
        manager.update(4.99)
        assert t1.running
        assert t1.alive
        assert param['stage'] == 2
        manager.update(5.01)
        assert not t1.running
        assert not t1.alive
Ejemplo n.º 8
0
    def test_propagate_exception(self):
        def throws_exception(task):
            yield 0.0
            raise Exception()

        def receives_exception(task, manager):
            yield manager.add(throws_exception)

        manager = task.TaskManager(0.0)
        t = manager.add(receives_exception, manager)
        manager.update(0.1)
        assert not t.alive
        assert not t.success
        assert t.exception
        assert not t.last_blocking.alive
        assert not t.last_blocking.success
        assert t.last_blocking.exception is t.exception
Ejemplo n.º 9
0
    def test_add_return_value(self):
        def do_task(task, l):
            l[0] += 1
            yield 0.1
            l[0] += 1
            yield 0.1
            l[0] += 1

        manager = task.TaskManager(0.0)
        l = [0]
        t = manager.add(do_task, l)
        assert l[0] == 0
        manager.update(0.0)
        assert l[0] == 1
        manager.update(0.1)
        assert l[0] == 2
        manager.remove(t)
        manager.update(0.2)
        assert l[0] == 2
Ejemplo n.º 10
0
    def test_catch_propagated_exception(self):
        def throws_exception(task):
            yield 0.0
            raise Exception()

        def receives_exception(task, manager):
            task.result = 0
            try:
                yield manager.add(throws_exception)
            except Exception:
                task.result = 123

        manager = task.TaskManager(0.0)
        t = manager.add(receives_exception, manager)
        manager.update(0.1)
        assert not t.alive
        assert t.success
        assert not t.last_blocking.alive
        assert not t.last_blocking.success
        assert t.result == 123
Ejemplo n.º 11
0
    def test_pending_task(self):
        def child(task):
            yield 1.0

        def parent(task, manager, param):
            yield 1.0
            t = manager.add(child)
            param['child'] = t
            yield 1.0

        manager = task.TaskManager(0.0)
        param = {'child': None}
        t1 = manager.add(parent, manager, param)
        assert t1.count == 0
        manager.update(0.0)
        assert t1.count == 1
        manager.update(1.0)
        assert t1.count == 2
        t2 = param['child']
        assert t2 is not None
        assert t2.count == 1
Ejemplo n.º 12
0
    def test_init(self):
        def do_task(task, a, b, c, *args, **kwargs):
            assert a == 123
            assert b == 456.789
            assert c == []
            assert args == ('foo', 'bar', 'baz')
            assert kwargs == {'apple': 12, 'orange': 34}
            c.append('Done')
            yield task.unit

        manager = task.TaskManager(0.0)
        c = []
        manager.add(do_task,
                    123,
                    456.789,
                    c,
                    'foo',
                    'bar',
                    'baz',
                    apple=12,
                    orange=34)
        manager.update(0.0)
        assert c == ['Done']
Ejemplo n.º 13
0
def run():
    # Unbuffer stdout
    sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)

    parser = op.OptionParser(usage=__usage__, option_list=options())
    opts, args = parser.parse_args()

    if len(args) > 1:
        parser.error("Invalid arguments: %s" % " ".join(args))

    basedir = args[0] if len(args) else os.getcwd()

    if opts.lib is not None:
        sys.path.insert(0, opts.lib)
    else:
        libdir = os.path.join(basedir, "lib")
        if os.path.isdir(libdir):
            sys.path.insert(0, libdir)

    configure_logging(opts)
    cfg = load_config(basedir, opts.cfg)
    mgr = task.TaskManager(cfg, basedir)
    mgr.execute()
Ejemplo n.º 14
0
    def test_finalize_removed(self):
        class Task(task.Task):
            finalized = False

            def run(self, a, b, c):
                assert a == 123
                assert b == 456.789
                assert c is None
                yield 0.1

            def finalize(self, a, b, c):
                assert a == 123
                assert b == 456.789
                assert c is None
                self.finalized = True

        manager = task.TaskManager(0.0)
        t = Task(123, 456.789, None)
        manager.add(t)
        assert not t.finalized
        manager.update(0.0)
        assert not t.finalized
        manager.remove(t)
        assert t.finalized
Ejemplo n.º 15
0
    def test_suspend_resume_epoch(self):
        class Task(task.Task):
            def run(self):
                yield 1.0

        manager = task.TaskManager(12345.0)
        t = Task()
        manager.add(t)
        assert t.running
        manager.update(12345.0)
        assert t.running
        manager.update(12345.5)
        assert t.running
        t.suspend()
        assert not t.running
        manager.update(12346.5)
        assert not t.running
        t.resume()
        assert t.running
        manager.update(12346.99)
        assert t.running
        manager.update(12347.01)
        assert not t.running
        assert t.finished
Ejemplo n.º 16
0
    def test_taskinfo(self):
        class Task(task.Task):
            finalized = False

            def run(self):
                assert self.count == 1
                assert self.time < 0.1
                yield 0.1
                assert self.count == 2
                assert self.time >= 0.1 and self.time < 0.11
                last = self.time
                yield 0.1
                assert self.count == 3
                assert self.time >= 0.2 and self.time < 0.21
                assert (self.time - last) - self.dtime < 1.0e-5

            def finalize(self):
                self.finalized = True

        manager = task.TaskManager(12345.0)
        t = Task()
        manager.add(t)
        run_manager(manager, 0.5)
        assert t.finalized
Ejemplo n.º 17
0
    def test_last_blocking(self):
        class BlockingTask(task.Task):
            def run(self):
                yield 0.1

        class Task(task.Task):
            def run(self, manager, blocking_task):
                yield manager.add(blocking_task)
                assert self.last_blocking is blocking_task

        manager = task.TaskManager(0.0)
        b = BlockingTask()
        assert b.alive
        t = Task(manager, b)
        manager.add(t)
        # Call manager.update() twice as a single call would not start running
        # the blocking task.
        # TODO: Consider running a blocking task immediately if the manager is
        # the same.
        manager.update(0.1)
        manager.update(0.2)
        assert not b.alive
        assert b.success
        assert t.last_blocking is b
Ejemplo n.º 18
0
    def test_init_method(self):
        class ClassA(object):
            done = False

            def do_task(self, task):
                self.done = True
                yield task.unit

        class ClassB(object):
            done = False

            def do_task(self, task):
                self.done = True
                yield task.unit

        manager = task.TaskManager(0.0)
        a, b = ClassA(), ClassB()
        manager.add(a.do_task)
        manager.add(b.do_task)
        assert not a.done
        assert not b.done
        manager.update(0.0)
        assert a.done
        assert b.done
Ejemplo n.º 19
0
Archivo: alto.py Proyecto: ygexe/alto
def main(confidence=CONFIDENCE, responsiveness=RESPONSIVENESS):
    '''Runs all the tasks required for Alto.

    Uses a TaskManager to start the tasks, check that they are alive
    and as a messaging bus.

    Blinks the LED on any errors.

    Emits:
      System.started()
    '''
    task_manager = task.TaskManager()
    GPIO.setmode(GPIO.BCM)

    # Set up the LED first as it is used if there are any errors.
    set_led = set_up_led(task_manager, LED_PIN)

    try:
        # Start the output tasks first and input tasks last.
        # This should ensure that all the bindings are in place
        # before the inputs are processed.
        task_manager.start(imprint_engine.ImprintEngineTask, confidence,
                           responsiveness)
        task_manager.start(servo_handler.ServoHandler, SERVO_CFG)
        task_manager.start(ui.AltoUI)
        task_manager.start(ButtonHandler)
        set_up_buttons(task_manager, BUTTON_PINS)

        # Indicate the system is ready by turning on the LED.
        task_manager.emit('System.started')
        set_led(1)

        # Run forever
        task_manager.process_messages()
    except KeyboardInterrupt:
        pass
    except Exception as exc:
        logging.exception('Error in main')

        # Emit a blink error code
        if isinstance(exc, picamera.PiCameraError):
            # Camera problems are shown as two blinks.
            blinks = 2
        else:
            # All other problems (most likely the TPU isn't conencted)
            # are shown as three blinks.
            blinks = 3

        set_led(0)
        time.sleep(1)
        # Emit the pattern 10 times.
        for _ in range(10):
            for _ in range(blinks):
                set_led(1)
                time.sleep(0.15)
                set_led(0)
                time.sleep(0.10)
            # Pause between sets.
            time.sleep(0.5)
    finally:
        # Clean up the tasks and turn the LED off on exit.
        task_manager.terminate()
        set_led(0)
        GPIO.cleanup()