pfalcon / pycopy-lib

Standard library of the Pycopy project, minimalist and light-weight Python language implementation
https://github.com/pfalcon/pycopy
Other
246 stars 70 forks source link

uasyncio: How to create tasks so they will be run immediatelly? #54

Closed iBobik closed 4 years ago

iBobik commented 4 years ago

I try to control two relays with different delays concurently. Each of relay has own config and own task.

from machine import Pin
import uasyncio as asyncio
loop = asyncio.get_event_loop()
import utime
import logging
log = logging.getLogger("relay")
import events

# Load config and initiate pins
import config
relays = config.modules['relay']['relays']
for r in relays:
    r.update({
        'pin_obj': Pin(r['pin'], Pin.OUT)
    })

@events.on('card.card_validated')
def on_card_validated(card):
    for config in relays:
        loop.create_task(relay_task(config, card))

async def relay_task(config, card):
    if 'open_after' in config:
        log.info("Will open relay on pin %d in %d seconds", config['pin'], config['open_after'])
        await asyncio.sleep(config['open_after'])

    log.info("Opening relay on pin %d for %d seconds", config['pin'], config['close_after'])
    config['pin_obj'].on()
    await asyncio.sleep(config['close_after'])
    config['pin_obj'].off()

This code works, but there is delay few seconds between create_task and relay_task start. I need to have the delay up to one second.

Pycopy v3.0.6 Modules has current versions from PyPi.

Is it bug or I use it incorrectly?

Thank you for help.

pfalcon commented 4 years ago

Tasks do run immediately (or more specifically, ASAP).

From a quick look, I don't see something obviously wrong with the above code, but it's neither minimal nor complete case (http://sscce.org/). What steps did you take to investigate and debug the issue?

pfalcon commented 4 years ago

No response, assuming issue is resolved. Closing. For future cases, please follow guidelines in the link posted.

iBobik commented 4 years ago

Sorry, hobby project was postponed until I will have a time for it again :-)

    1. 2020 v 9:29, Paul Sokolovsky notifications@github.com:

No response, assuming issue is resolved. Closing. For future cases, please follow guidelines in the link posted.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/pfalcon/pycopy-lib/issues/54#issuecomment-631932161, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAEV6WC5P4CIQVJ3A43GDDDRSTJ47ANCNFSM4MGUSIJQ.

pfalcon commented 4 years ago

Sorry, hobby project was postponed until I will have a time for it again :-)

That's ok, feel free to resubmit/reopen when a testcase is available.

I'm from my side polishing/extending existing tests for uasyncio, and make sure they're actually run regularly.