Skip to content

Latest commit

 

History

History
119 lines (103 loc) · 3.29 KB

221102.tasks.md

File metadata and controls

119 lines (103 loc) · 3.29 KB

2022-11-02

https://discordapp.com/channels/918498540232253480/918498540232253483/1037485396596117596

bmentink

Question about the main: block. If you start a bunch of tasks, can you just do nothing further, or does there need to be a while true: block at the end of it, and if so, does it need to contain a sleep. I have seen examples of both. Cheers.

floitsch

There are two kinds of tasks: foreground and background tasks.
If you create a task with task:: it becomes a foreground task, and the program will wait for it to finish.
If you use task --background:: then it's a background task, and the program will exit if all other foreground tasks (including main) terminate.
You can try this yourself:

main:
  task --background::
    sleep --ms=10_000
    print "done"
  print "main done"

Run this with the --background and then without.

bmentink

What i currently have is:

main:
  sleep --ms=1000
  print "Starting all tasks"

  task:: publish_task
  task:: irrigate_task
  //task:: debug_task
  task:: deep_sleep_task 

  while true:
    sleep --ms=5000

floitsch

I'm pretty sure you don't need the while true loop at the end.
There is also a trick, where you wait for something that will never happen:

import monitor

main:
  (monitor.Semaphore).down

In this case, the down will wait for the semaphore to go up, which will never happen. -> the task is stuck.
This can sometimes be useful (especially for tests).

Geoff

Unlike bmentink, I WANT to end the main: block. But the last lines are:

  server.listen network port:: | request writer |
    handle request writer

I tried:

  server.listen network port:: | request writer |
    handle request writer
    if request.path == "setssid.html":
      config := { 
        wifi.CONFIG_SSID: form_data["ssid"],
        wifi.CONFIG_PASSWORD: form_data["pass"],
      }
      store := device.FlashStore
      store.set "system/wifi" config
      return // exit captive_portal/main:

but jag complains:

captive_portal.toit:229:7: error: Can't explicitly return from within a lambda
      return // exit captive_portal/main:

I'm sorry I'm not very good with lambdas. It appears I have to exit the lambda before I can exit main:.

floitsch

You can start the server in its own task
Oh. Wait.
Can you call server.close instead of the return?
Not sure if the server has such a method but if yes it might work.

Geoff

So like:

 server.listen network port:: | request writer |
    handle request writer
    if request.path == "setssid.html": server.close

do more stuff

floitsch

Yes
Alternatively:

server_task := null
server_task = task:
  server.listen...
     server_task.cancel

Just some of the ideas I have.
Could easily be that there are better ways. Also depends a bit on the http library which I'm not super familiar with.

Geoff

captive_portal.toit:239:47: error: Class 'Server' does not have any method 'close'
    if request.path == "setssid.html": server.close

I'll try your second idea.

floitsch

Also. If you just want to stop the program, you can call exit 0

Geoff

Changed "handle" function to return boolean. Now this works perfectly:

server.listen network port:: | request writer |
    if handle request writer: exit 0