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

Too Long To Yeild

Started by LittleBlackA, 11 October 2014 - 08:34 AM
LittleBlackA #1
Posted 11 October 2014 - 10:34 AM
This is the pastebin: http://pastebin.com/WAPJN2uQ

I am trying to infinite loop it so i can get realtime data but i keep getting Too long to yeild or something similar.

That is just an API.
SquidDev #2
Posted 11 October 2014 - 11:01 AM
From here:

Yield protection is a ComputerCraft-specific issue, due to the nature of the Java side handling of the Lua environment for the computers. This message means that your program has run for ten seconds without yielding, which usually means you have an infinite loop that is constantly running. You can fix this simply by adding a sleep(0), but it's better to determine how often your code actually needs to run and use an os.pullEvent() to wait for the events it needs. For instance, a loop which looks at changes in redstone state could include an os.pullEvent("redstone") to wait for changes in redstone state before iterating through the loop again.

To simplify, the program can't run for more than 10 seconds before yielding…

The best solution is something like this:


local oldTime = os.time()
local function sleepCheckin()
    local newTime = os.time()
    if newTime - oldTime >= (0.020 * 1.5) then
	    oldTime = newTime
	    sleep(0)
    end
end

And just call sleepCheckin() once per loop. All it does it sleep for 0 seconds if it needs to.
LittleBlackA #3
Posted 11 October 2014 - 12:13 PM
I just realised I screwed up the whole question.
The output spazzes out the screen updating real time and updating the color but then it stops updating real time and comes up with: Periphral(random number usually 68): Too long without yielding. I am try to achieve the screen to update information real time so I know the Flux per tick.

Sorry I was in a hurry when typing the topic.

Edit: You understood what I was saying anyway thank you. I will test this tomorrow and updating you on the status than. Thanks
Edited on 11 October 2014 - 10:19 AM
ElvishJerricco #4
Posted 11 October 2014 - 04:45 PM
Yea you still probably don't want to be doing this real time. You want to decide on an update speed. Most people are happy with once a second. Throw your code in a loop that sleeps for a second each iteration. Running an infinite loop and trying to get around the yield error is bad practice and can be detrimental to a server, especially if it's in a loop that draws to a monitor, which has to push screen updates to clients each time.

EDIT: I relaize that the sleepCheckIn code above is my own from Grin but that's different =P It's doing a one time, long-running task.
Edited on 11 October 2014 - 02:50 PM
Lyqyd #5
Posted 11 October 2014 - 07:59 PM
Yeah, power usage code doesn't really need to run more than once or twice per second at the most. Please be kind to the servers you're on.
LittleBlackA #6
Posted 12 October 2014 - 12:49 AM
Oh ok, so i would do sleep(1) instead of sleep(0) correct?

Edit: I have updated the code to have the sleepCheckin() function but i am still suffering from dat error :/
Edited on 12 October 2014 - 12:32 AM
LittleBlackA #7
Posted 16 October 2014 - 08:24 AM
Bump?
Bomb Bloke #8
Posted 16 October 2014 - 09:37 AM
Are you sure that's the error you're getting? To my eye, you've got at least one mismatched end statement in there (fix your indentation!), an incorrect comparator on line 44, and… well, I'm not sure how this would run at all…

Sort those two issues out and see how things go from there.

In regards to the sleep duration, for the sake of getting the script to run, it doesn't matter so long as your script is yielding on a regular basis. How long it yields for isn't relevant (though longer yields will generally lead to better performance across the other computers on your server).
LittleBlackA #9
Posted 16 October 2014 - 11:24 AM
Oh yer i kept screwing around and now im getting that :/ but i will fix it soon.
Dragon53535 #10
Posted 16 October 2014 - 11:44 AM
Well if you want a lot of help, it helps to post your code so we know what we're working with.