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

simultaneous tasks?

Started by digitalEntity, 13 February 2015 - 10:42 PM
digitalEntity #1
Posted 13 February 2015 - 11:42 PM
Is it possible for programs to do more than one thing at once? For instance, could I have a program rendering different background images every 10 seconds and still have that program pull user input? Maybe control would temporarily shift to the rendering every 10 seconds?
digitalEntity #2
Posted 13 February 2015 - 11:54 PM
possibly something involving coroutines passing back and forth on a timer?
digitalEntity #3
Posted 14 February 2015 - 12:35 AM
nevermind… I just found the Parallel api -_-/>…
Bomb Bloke #4
Posted 14 February 2015 - 12:50 AM
There's also the option of a loop along the lines of the second example here.
digitalEntity #5
Posted 14 February 2015 - 03:11 AM
Ok… So I seem to have hit a problem… I've almost had it running with parallel.waitForAny… but then when I ran them, it told me it ran too long without yielding…

But of course it ran too long without yielding, I'm making an operating system that doesn't shut down, and a constant background renderer… neither of them actually returns… any ideas?
HPWebcamAble #6
Posted 14 February 2015 - 03:24 AM
Ok… So I seem to have hit a problem… I've almost had it running with parallel.waitForAny… but then when I ran them, it told me it ran too long without yielding…

But of course it ran too long without yielding, I'm making an operating system that doesn't shut down, and a constant background renderer… neither of them actually returns… any ideas?

adding a sleep(0) in the loop should prevent the error

But it would be better to design the rendering function to only do anything when there is new information to put on the screen.

For example, an input handling function could see that a user did something, and queue an event that the render would react to.
Edited on 14 February 2015 - 02:26 AM
digitalEntity #7
Posted 14 February 2015 - 03:36 AM
Well I'm currently just testing for a future extension of a current project in which one VERY extensive OS controls the entire computer and runs multiple background functions constantly while allowing the user to still control the system… the background render it's intended to show the current state of some of these functions all at once. To the oblivious user, it's nothing but a weird dot in the middle of the screen surrounded by 4 2x2 squares that randomly highlight one of their pixels, but to those that know what's going on… the program is essentially thinking… really hard… and some time in the future, the render will happen every 0.05 second (1 game tick; fastest possible speed)

Edit: P.s. I was wrong, the render function has a sleep(10) inside the end of a while true do loop…
Edit 2: and the other function is still unfinished, so it just loops right back to the standard OS
Edited on 14 February 2015 - 02:42 AM
Bomb Bloke #8
Posted 14 February 2015 - 05:41 AM
Bear in mind that "yielding" is completely different to "returning". Your system yields whenever you go to pull an event (os.pullEvent() is basically a fancy way of calling coroutine.yield()). Sleeping yields (because it pulls a timer event), as does most anything else that triggers a "pause".

ComputerCraft only ones one process at a time, so while one coroutine is active, no others are - including processes running on other computers! Thus if a given process goes more than 10 seconds without a yield, it gets killed to prevent it from halting everyone else's runtime.
digitalEntity #9
Posted 14 February 2015 - 12:24 PM
so… say I put 2 functions into parallel.waitForAny (and that they function perfectly), and both of them yielded often, but neither ever returned. Would that cause errors?

I'm sorry I can't give you definitive [.CODE] examples, but if I'm not sleeping or doing school work, I'm usually working on it constantly.
KingofGamesYami #10
Posted 14 February 2015 - 02:20 PM
so… say I put 2 functions into parallel.waitForAny (and that they function perfectly), and both of them yielded often, but neither ever returned. Would that cause errors?

No. As long as you yield, they will be fine. Same rule as making a program: yields are necessary.