https://toitlang.org/ logo
Stop a blocking sleep
# help
p
Hi, lets say we have this task task:: pin := gpio.Pin 2 --output while true: pin.set 1 sleep --ms=2000 pin.set 0 sleep --ms=2000 After a button press(another task or the same task) we need to respond immediatelly, so the sleep here is inapropriate. I have tried with "monitor" Mutex await try-await but I cannot do it. Ok there is the possibility of a tight loop with "sleep --ms=50" but I have hoped for something more elegant
f
Have a look at `Task.group`: https://libs.toit.io/core/task/class-Task#group(2%2C0%2C0%2Crequired)
You can then have a sleep in one lambda and another lambda to listen to a channel (or so)
p
So, it is the equivalent of golang "select" ?
f
Close enough. You tell the group how many lambdas need to complete before it kills the others. So it's more flexible. On the other hand, it's probably more expensive than a select in go
p
As you said the other lamdas are killed. This is good, but are there any resources left ? (memory leak) I dont know the internals of toit VM so I have to ask
f
Toit has a GC. That said: if you know of resources that should be closed, do that in a
finally
. Those are still executed; even when the task is killed.
p
Thank you very much ! I will try this at night.