This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Exerro's profile picture

do coroutines end when the program stops

Started by Exerro, 11 January 2013 - 10:01 AM
Exerro #1
Posted 11 January 2013 - 11:01 AM
this is probably a dumb question but do coroutines that are started in a certain program terminate when that program stops because i want to make a program that runs in the background and collects user data but not sure how to do it without making a custom shell
Kingdaro #2
Posted 11 January 2013 - 11:05 AM
They probably just stop existing when you turn off the computer, as does every other variable/object.
Lyqyd #3
Posted 11 January 2013 - 11:08 AM
Depends on what you have managing the coroutines. Things like Gopher's goroutine API allow you to spawn peer coroutines that will outlast the currently running program. The standard CraftOS setup can't do this; you have to either use a custom coroutine manager (as mentioned above), or mess around with parallel calls to begin a new shell instance if you want to be able to run something in the background and still use the computer for other things.
immibis #4
Posted 11 January 2013 - 12:40 PM
If you're using parallel, they end when the parallel call returns. If you use the raw coroutine API, it won't run at all unless something keeps resuming it. There are APIs that will let you run coroutines in the background (Lyqyd mentioned one called goroutine) but you can't do it in standard CC.
You can run your own shell, however:

parallel.waitForAny(
  function()
    your code here
  end,
  function()
    shell.run("shell")
  end
end)
GopherAtl #5
Posted 11 January 2013 - 06:25 PM
goroutine api can do this, but you have to launch goroutine in your startup file, but that's fairly easy to do. There's example code in the goroutine API thread.
KaoS #6
Posted 21 March 2013 - 06:11 AM
goroutine api can do this, but you have to launch goroutine in your startup file, but that's fairly easy to do. There's example code in the goroutine API thread.

well if advertising is allowed KaoShell does this too :)/> it's in here

I think the best way of summarizing this is that with a custom loader you can keep them running after program termination, without it the program will never end until the coroutine does so you cannot have a coroutine outlasting a program

EDIT: ahh sorry I necro'd :mellow:/> not intentional. reporting it now… I browse the activity list to find interesting topics as many users are reading them and didn't realize it was old

EDIT 2: thanks Lyqyd, I've managed to avoid getting any warns so far and am a little paranoid about it. I should also come clean and say I actually mainly browse the activity list to find topics that are about to be replied to by people that interest me (mysticT and Espen have great explanations, always worth it to read and of course Cloudy's posts are hilarious half the time)
Edited on 21 March 2013 - 05:27 AM
Lyqyd #7
Posted 21 March 2013 - 06:22 AM
Adding good and valid content often makes bumps acceptable. I'm not worried about this one; I'm sure the topic (with more complete information now) will die again.