Exemplo n.º 1
0
async def run_cancel_test():
    loop = asyncio.get_event_loop()
    await asyn.NamedTask('foo 0', foo, 0)
    loop.create_task(asyn.NamedTask('foo 1', foo, 1)())
    loop.create_task(bar())
    await asyncio.sleep(5)
    await asyn.NamedTask('foo 2', foo, 2)
    await asyn.NamedTask('foo 4', foo, 4)
    loop.create_task(asyn.NamedTask('foo 3', foo, 3)())
    await asyncio.sleep(5)
Exemplo n.º 2
0
async def run_cancel_test2():
    barrier = asyn.Barrier(3)
    loop = asyncio.get_event_loop()
    loop.create_task(asyn.NamedTask('rats_1', rats, 1, barrier=barrier)())
    loop.create_task(asyn.NamedTask('rats_2', rats, 2, barrier=barrier)())
    print('Running two tasks')
    await asyncio.sleep(10)
    print('About to cancel tasks')
    await asyn.NamedTask.cancel(
        'rats_1')  # These will stop when their wait is complete
    await asyn.NamedTask.cancel('rats_2')
    await barrier  # So wait for that to occur.
    print('tasks were cancelled')
Exemplo n.º 3
0
def set_property(req, resp):
    global led_status
    global start_property
    status = 404
    answer = ''
    if req.method == "POST":
        yield from req.read_form_data()
        log.info("{} {} {} {}".format(time2str(), 'Команда:', 'set_property',
                                      req.form))
        id = req.form["id"]
        if id in start_property:
            for prop_name in start_property[id]['property']:
                if prop_name == 'hsl':
                    start_property[id]['property']['hsl'] = (int(
                        req.form['h']), int(req.form['s']), int(req.form['l']))
                else:
                    start_property[id]['property'][prop_name] = int(
                        req.form[prop_name])
            if led_status is not None:
                yield from asyn.NamedTask.cancel(led_status)
                led_status = "re{}".format(led_status)
                loop.create_task(
                    asyn.NamedTask(led_status, start_property[id]['function'],
                                   start_property[id]['property'])())
                log.info("{} {} {}".format(time2str(), 'Задача запущена:',
                                           led_status))
        else:
            answer = "Error! id {} not found!".format(req.form["id"])
        if not answer:
            status = 200
            answer = "id {} done!".format(req.form["id"])
    else:
        answer = "Error! Method not found!"
    yield from picoweb.start_response(resp, status=status)
    yield from resp.awrite(answer)
Exemplo n.º 4
0
def start(req, resp):
    global led_status
    if req.method == "POST":
        yield from req.read_form_data()
        log.info("{} {} {} {}".format(time2str(), 'Команда:', 'start',
                                      req.form))
        id = req.form["id"]
        if id in start_property:
            if led_status or id == "0":
                yield from asyn.NamedTask.cancel(led_status)
                log.info("{} {}".format(time2str(), 'Задача остановлена'))
                led_init.led_off()
                led_status = None
            else:
                led_status = start_property[id]['name']
                loop.create_task(
                    asyn.NamedTask(led_status, start_property[id]['function'],
                                   start_property[id]['property'])())
                log.info("{} {} {}".format(time2str(), 'Задача запущена:',
                                           led_status))
        else:
            yield from picoweb.start_response(resp, status=404)
            yield from resp.awrite("id {} not found!".format(req.form["id"]))
            return
    yield from picoweb.start_response(resp)
    yield from resp.awrite("id {} done!".format(req.form["id"]))
Exemplo n.º 5
0
async def run_named_test(loop):
    res = await asyn.NamedTask('not cancelled', add_one_named, 99)
    print('Result: ', res)
    loop.create_task(asyn.NamedTask('print nums', print_nums_named, res)())
    await asyn.sleep(7.5)
    asyn.NamedTask.cancel('not cancelled')  # Nothing to do: task has finished
    asyn.NamedTask.cancel('print nums')  # Stop the continuously running task
    print('Done')
Exemplo n.º 6
0
async def run_cancel_test6(loop):
    for name in ('complete', 'cancel me'):
        loop.create_task(asyn.NamedTask(name, cant60, name)())
    loop.create_task(asyn.Cancellable(cant61)())
    await asyncio.sleep(4.5)
    print('Cancelling task \"{}\". 1.5 secs latency.'.format(name))
    await asyn.NamedTask.cancel(name)
    await asyncio.sleep(7)
    name = 'cancel wait'
    loop.create_task(asyn.NamedTask(name, cant60, name)())
    await asyncio.sleep(0.5)
    print('Cancelling task \"{}\". 1.5 secs latency.'.format(name))
    t = time.ticks_ms()
    await asyn.NamedTask.cancel('cancel wait', nowait=False)
    print('Was cancelled in {} ms'.format(time.ticks_diff(time.ticks_ms(), t)))
    print('Cancelling cant61')
    await asyn.Cancellable.cancel_all()
    print('Done')
Exemplo n.º 7
0
 async def start(self, loop):
     loop.create_task(asyn.Cancellable(
         self.foo, 1)())  # 3 instances in default group 0
     loop.create_task(asyn.Cancellable(self.foo, 2)())
     loop.create_task(asyn.Cancellable(self.foo, 3)())
     loop.create_task(asyn.NamedTask('my bar', self.bar, 4, y=42)())
     await asyncio.sleep(4.5)
     await asyn.NamedTask.cancel('my bar')
     await asyn.Cancellable.cancel_all()
     await asyncio.sleep(1)
     print('Done')
Exemplo n.º 8
0
async def run_cancel_test3():
    barrier = asyn.Barrier(2)
    loop = asyncio.get_event_loop()
    loop.create_task(asyn.NamedTask('cant3', cant3, barrier=barrier)())
    print('Task cant3 running status', asyn.NamedTask.is_running('cant3'))
    await asyncio.sleep(3)
    print('Task cant3 running status', asyn.NamedTask.is_running('cant3'))
    print('About to cancel task')
    await asyn.NamedTask.cancel('cant3')
    print('Cancelled')
    print('Task cant3 running status', asyn.NamedTask.is_running('cant3'))
    await barrier
    print('tasks were cancelled')
Exemplo n.º 9
0
 async def start(self, loop):
     loop.create_task(asyn.Cancellable(self.foo, 1)())  # 3 instances in default group 0
     loop.create_task(asyn.Cancellable(self.foo, 2)())
     loop.create_task(asyn.Cancellable(self.foo, 3)())
     loop.create_task(asyn.NamedTask('my bar', self.bar, 4)())
     print('bar running status is', asyn.NamedTask.is_running('my bar'))
     await asyncio.sleep(4.5)
     asyn.NamedTask.cancel('my bar')
     print('bar instance scheduled for cancellation.')
     await asyn.Cancellable.cancel_all()
     print('foo instances have been cancelled.')
     print('bar running status is', asyn.NamedTask.is_running('my bar'))
     print('Done')
Exemplo n.º 10
0
    def on_active_msg(self, topic, payload, retained):
        if payload == FALSE:
            if self.active:
                asyn.launch(asyn.NamedTask.cancel, (_SENSOR,))
                self.active = False
        elif payload == TRUE:
            if not self.active:
                self.active = True
                loop = get_event_loop()
                loop.create_task(
                    asyn.NamedTask(_SENSOR, self.pir_sensor)()
                )
        else:
            return

        self.active_property.data = payload
Exemplo n.º 11
0
    def callback(self, topic, payload, retained):
        if b"active" in topic:
            if payload == FALSE:
                if self.active:
                    asyn.launch(asyn.NamedTask.cancel, ("pir_sensor", ))
                    self.active = False
            elif payload == TRUE:
                if not self.active:
                    self.active = True
                    loop = get_event_loop()
                    loop.create_task(
                        asyn.NamedTask("pir_sensor", self.pir_sensor)())
            else:
                return

            self.pir_property.data = payload
            if retained:
                self.pir_sensor.update_delta()