I have looked through lua-users Sleep Function reference in an effort to find a non-busy waiting solution to the sleep problem and I'm not happy with any of them. Nonetheless I attempted using several to provide a delay at the end of a function that uses tail calls.
Normally I wouldn't use tail calls but since lua doesn't leave a stack behind for tail calls it suits me.
Unfortunately I see my processor spike to about 20% use and the program becomes immediately unresponsive without any flushing of output once it begins.
The problem (simplified) looks like this:
function myFunc ()
-- do some stuff
-- lots of snazzy logic and function calls
-- heck, throw in a few prints
print "Going to sleep"
-- sleep for a bit
os.execute("sleep 10")
print "Waking up"
-- tail call
return myFunc()
end
I have tried the socket select method, os.execute and of course busy waiting. Of these only busy waiting gives the expected behaviour.
Are these other non-busy waiting solutions also non blocking? That is do they allow the tail call to be processed despite the delays?
How can I flush the output and have the function wait 10 seconds before resuming without busy waiting?