Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
iBobik opened this issue Apr 13, 2020 · 4 comments
Closed

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

iBobik opened this issue Apr 13, 2020 · 4 comments

Comments

@iBobik
Copy link

iBobik commented Apr 13, 2020

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
Copy link
Owner

pfalcon commented Apr 15, 2020

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
Copy link
Owner

pfalcon commented May 21, 2020

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

@pfalcon pfalcon closed this as completed May 21, 2020
@iBobik
Copy link
Author

iBobik commented May 21, 2020 via email

@pfalcon
Copy link
Owner

pfalcon commented May 26, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants