Difference between revisions of "User:Minozake"
(Added safe terminate code) |
(→Termination catch for infinite loop daemons) |
||
(One intermediate revision by the same user not shown) | |||
Line 29: | Line 29: | ||
parallel.waitForAny(doPullEvent, doRunProg) | parallel.waitForAny(doPullEvent, doRunProg) | ||
</pre> | </pre> | ||
+ | |||
+ | It works by parallelizing both functions and waiting for one of them to exit. When one function exits, the program exits. Since it is impossible for the loop to exit, it rests on catching the terminate event, which is already blocking until the event is caught. You could also easily modify this to take a keypress event instead if that is what you prefer, but that will be an exercise left up to the reader. | ||
An example for use would be a GPS daemon. Unfortunately, a GPS host computer at startup that just has `shell.run("gps", "host")` is prone to failures due to timing problems or thunderstorms since they depend on at least 4 other computers to get up and running properly, and it will also be a problem for entire networks that base their hosting off of 4 main computers. A workaround is to hardcode all of the computers, but that requires manual or more complex auto setups. Running `shell.run("gps", "host")` in a startup infinite loop is a fix, but it also blocks all other operation of the computer, especially if trying to debug. Adding the termination catch so that the script is safe to run on startup is recommended and optimal. | An example for use would be a GPS daemon. Unfortunately, a GPS host computer at startup that just has `shell.run("gps", "host")` is prone to failures due to timing problems or thunderstorms since they depend on at least 4 other computers to get up and running properly, and it will also be a problem for entire networks that base their hosting off of 4 main computers. A workaround is to hardcode all of the computers, but that requires manual or more complex auto setups. Running `shell.run("gps", "host")` in a startup infinite loop is a fix, but it also blocks all other operation of the computer, especially if trying to debug. Adding the termination catch so that the script is safe to run on startup is recommended and optimal. | ||
I will leave it as an exercise to the reader to figure out how to make this into a flexible script and make it loadable as an API. | I will leave it as an exercise to the reader to figure out how to make this into a flexible script and make it loadable as an API. |
Latest revision as of 20:03, 12 May 2012
Miscellaneous thingies
So, I'm using this to post miscellaneous code snippets for other people to use until I find a wiki page for it.
Termination catch for infinite loop daemons
So, let's say you want the source of a program in the form of
while true do shell.run("prog", "arg1") end
It's all fine and good, except for a huge potential problem. If you want to terminate it, you will just restart the program being run in the loop. For computers with the program in their startup, it will require the computer be removed and replaced, and that will destroy the contents saved onto its disk. That's where more complex code comes in handy. The code follows the form:
function doPullEvent () os.pullEventRaw("Terminate") print("Terminate caught. Exiting.") end function doRunProg () while true do shell.run("gps", "host") end end parallel.waitForAny(doPullEvent, doRunProg)
It works by parallelizing both functions and waiting for one of them to exit. When one function exits, the program exits. Since it is impossible for the loop to exit, it rests on catching the terminate event, which is already blocking until the event is caught. You could also easily modify this to take a keypress event instead if that is what you prefer, but that will be an exercise left up to the reader.
An example for use would be a GPS daemon. Unfortunately, a GPS host computer at startup that just has `shell.run("gps", "host")` is prone to failures due to timing problems or thunderstorms since they depend on at least 4 other computers to get up and running properly, and it will also be a problem for entire networks that base their hosting off of 4 main computers. A workaround is to hardcode all of the computers, but that requires manual or more complex auto setups. Running `shell.run("gps", "host")` in a startup infinite loop is a fix, but it also blocks all other operation of the computer, especially if trying to debug. Adding the termination catch so that the script is safe to run on startup is recommended and optimal.
I will leave it as an exercise to the reader to figure out how to make this into a flexible script and make it loadable as an API.