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

In parallel with CraftOS

Started by cmdpwnd, 04 July 2015 - 02:30 AM
cmdpwnd #1
Posted 04 July 2015 - 04:30 AM
Is it possible to run a program in parallel with CraftOS? What I mean by this is say I have a program that listens for a modem_message and will print that message upon receiving it, what I want to do is have that program running in parallel with CraftOS itself so that I can be using CraftOS and see any data that I receive via modem_message in the terminal. An example is say I'm typing the command 'edit file'(note I haven't pressed Enter) and my program receives a message, the terminal would resemble this

CraftOS 1.7

>edit file
Received Message: Hello!
Bomb Bloke #2
Posted 04 July 2015 - 05:01 AM
multishell allows you to do this, although it separates the display of each script you run into different tabs (only one of which can be viewed at a time). Having them all on-screen at once, at least in any useful fashion, is rather more complex; though I gather some of the more graphical alternate OSes can do that.
cmdpwnd #3
Posted 04 July 2015 - 05:25 AM
multishell allows you to do this, although it separates the display of each script you run into different tabs (only one of which can be viewed at a time). Having them all on-screen at once, at least in any useful fashion, is rather more complex; though I gather some of the more graphical alternate OSes can do that.

two problems,
1. can I do this without it displaying the tabbing feature, im essentially wanting it to be exempt from view.

2. second running something extremely simple like bg file

'file'
——-
while true do
print('1')
end

hangs the computer until it finally switches tabs and then will give an error "too long without yielding" after so long
Edited on 04 July 2015 - 03:53 AM
Bomb Bloke #4
Posted 04 July 2015 - 08:49 AM
1. can I do this without it displaying the tabbing feature, im essentially wanting it to be exempt from view.

multishell can't, no. Search for a different OS if you want different behaviour.

2. second running something extremely simple like bg file

'file'
——-
while true do
print('1')
end

hangs the computer until it finally switches tabs and then will give an error "too long without yielding" after so long

When a computer/turtle starts running code, ComputerCraft starts a ten second timer. If that code doesn't yield before that timer ends then ComputerCraft will either crash the script or the whole computer (depending on the nature of the functions your script is calling). After each yield, any other systems waiting to run code may do so, then after they've all yielded, processing continues with a new time limit.

The reason why is that running your code chews up valuable server processing power, and so it shouldn't be able to monopolise it. In fact, only ONE CC device can run code at a time: While one is doing something, none of the others can do anything until it yields.

Whether or not it takes more than ten seconds for your code to execute has a little to do with the power of the Minecraft server it's running on, and a lot to do with how often you allow your code to yield. Pulling events (eg getting typed characters or checking timers) triggers a yield, and many commands (eg turtle movements, sleeping, or getting text input from the user) have to pull events to work anyway. Basically, anything that triggers a pause is pulling an event in order to do it, and in order to pull an event the code yields.

In your loop, you aren't yielding at all, and so it'll crash in short order (regardless as to whether you run it in a separate multishell tab, or if you just run it "normally"). Add a "sleep(5)" or somesuch.
cmdpwnd #5
Posted 04 July 2015 - 09:34 AM
multishell can't, no. Search for a different OS if you want different behaviour.
Thanks bomb, the reason I was asking originally is because im making an api that needs to run side-by-side with CraftOS upon os.loadAPI('file') (active code within the api initiated as coroutines instead of just functions)which was proving very difficult without implementing a custom startup script or building a resource pack to make a custom rom/startup. The idea is that when you load the API it will create these coroutine processes and return control to CraftOS so that it can finish loading. Then when you are using the api it is deriving many values from said coroutine
Edited on 04 July 2015 - 07:35 AM