This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
Run program as coroutine
Started by EveryOS, 14 March 2016 - 12:57 PMPosted 14 March 2016 - 01:57 PM
I am making a multitasking os, and I need to run a program as a coroutine. is this possible? Other people have seemed to do it.
Posted 14 March 2016 - 02:15 PM
Actually, is this possible. Would it work to just call my terminal program, or what?
Posted 14 March 2016 - 02:47 PM
Posted 14 March 2016 - 04:34 PM
That didn't help…try this: http://www.computerc...-to-coroutines/
But I think I figured it out!!!
–shell–
_G.bash={}
program=1
bash.programs={
"program1",
"program2"}
function bash.nextProgram()
if program~=table.length(bash.programs) them
program=program+1
else
program=1
end
shell.run(bash.programs[program])
end
Of course, I'll have to add something to resume the program rather than reset it…
Edited on 14 March 2016 - 03:34 PM
Posted 14 March 2016 - 05:28 PM
If you can't understand Bomb Bloke's multitasking tutorial, I highly doubt you will be able to implement multitasking. You will have to use coroutines properly in order to do so.
Posted 14 March 2016 - 06:05 PM
Nah…If you can't understand Bomb Bloke's multitasking tutorial, I highly doubt you will be able to implement multitasking. You will have to use coroutines properly in order to do so.
Posted 14 March 2016 - 06:18 PM
function yay(whatever) – has to be a function
print(whatever..' noob')
end
–then we use coroutines to call it…
coroutine.create(yay)
w = coroutine.wrap(yay)
w('i am a')
print(whatever..' noob')
end
–then we use coroutines to call it…
coroutine.create(yay)
w = coroutine.wrap(yay)
w('i am a')
Posted 14 March 2016 - 06:40 PM
I need to run a program as a coroutine
Not a function!!
Not a function!!
Posted 14 March 2016 - 06:41 PM
Check out os.run for an example of loading a program up into a function for use.
Posted 14 March 2016 - 07:02 PM
Didn't workCheck out os.run for an example of loading a program up into a function for use.
–I tried–
–test file–
print('a')
coroutine.yield()
print('b')
–lua console–
cor=coroutine.create(os.run({},'test'))
But it just ignored the coroutine.yield
Posted 14 March 2016 - 07:06 PM
Lyqyd meant "look at how os.run does it" as in read the source code of os.run
Posted 14 March 2016 - 07:07 PM
There's an apis file for the os?
Posted 14 March 2016 - 07:29 PM
I tried coroutine.create(assert(loadfile('test'))) but it ignored the coroutine.yield()
Ahh. I get it now
I can do what java does, with a
main=function(multitasker.id)
end
Ahh. I get it now
I can do what java does, with a
main=function(multitasker.id)
end
Posted 14 March 2016 - 07:39 PM
In order to resume a couroutine, you first want to create one:
Oh my, func is a function. Where do I get it from?
It very simple, loadfile will do the job:
Hey, I don't want it to have access to the global environment. That is where setfenv comes into play:
The whole code looks like this:
Tadaaaaaa
coro = coroutine.create(func)
Oh my, func is a function. Where do I get it from?
It very simple, loadfile will do the job:
func = loadfile(path)
Hey, I don't want it to have access to the global environment. That is where setfenv comes into play:
setfenv(func, env)
The whole code looks like this:
func = loadfile(path)
setfenv(func, env)
coro = coroutine.create(func)
while true do
local event = coroutine.yield() -- like os.pullEvent
coroutine.resume(coro, unpack(event))
end
Tadaaaaaa
Posted 14 March 2016 - 07:52 PM
Yaay! (:=In order to resume a couroutine, you first want to create one:coro = coroutine.create(func)
Oh my, func is a function. Where do I get it from?
It very simple, loadfile will do the job:func = loadfile(path)
Hey, I don't want it to have access to the global environment. That is where setfenv comes into play:setfenv(func, env)
The whole code looks like this:func = loadfile(path) setfenv(func, env) coro = coroutine.create(func) while true do local event = coroutine.yield() -- like os.pullEvent coroutine.resume(coro, unpack(event)) end
Tadaaaaaa
Posted 14 March 2016 - 07:53 PM
So, I helped you? Or was that a scream of despair? :P/>
Edited on 14 March 2016 - 06:54 PM
Posted 14 March 2016 - 08:00 PM
I'm waiting to test it, but I think it'll workSo, I helped you? Or was that a scream of despair? :P/>
Posted 14 March 2016 - 08:04 PM
There's an apis file for the os?
No, some of it is in bios.lua (the file I linked) but most of it is defined on the Java side. This is the similar for the FS API and turtle API.
Anything which gets or sets data in the minecraft world or on the actual computer/internet has at least part of itself defined through Java.
Posted 15 March 2016 - 04:23 PM
In order to resume a couroutine, you first want to create one:coro = coroutine.create(func)
Oh my, func is a function. Where do I get it from?
It very simple, loadfile will do the job:func = loadfile(path)
Hey, I don't want it to have access to the global environment. That is where setfenv comes into play:setfenv(func, env)
The whole code looks like this:func = loadfile(path) setfenv(func, env) coro = coroutine.create(func) while true do local event = coroutine.yield() -- like os.pullEvent coroutine.resume(coro, unpack(event)) end
Tadaaaaaa
I'm having troubles
[attachment=2519:Screenshot 2016-03-15 at 12.22.50 PM.png]
When I run test itsself it is ok
Edited on 15 March 2016 - 03:26 PM
Posted 15 March 2016 - 04:36 PM
Never mind. SetFenv threw me offIn order to resume a couroutine, you first want to create one:coro = coroutine.create(func)
Oh my, func is a function. Where do I get it from?
It very simple, loadfile will do the job:func = loadfile(path)
Hey, I don't want it to have access to the global environment. That is where setfenv comes into play:setfenv(func, env)
The whole code looks like this:func = loadfile(path) setfenv(func, env) coro = coroutine.create(func) while true do local event = coroutine.yield() -- like os.pullEvent coroutine.resume(coro, unpack(event)) end
Tadaaaaaa
I'm having troubles
[attachment=2519:Screenshot 2016-03-15 at 12.22.50 PM.png]
When I run test itsself it is ok
Posted 15 March 2016 - 06:08 PM
And, there should be a loop that calls coroutine.yield regularly in the function.
Posted 15 March 2016 - 06:40 PM
And, there should be a loop that calls coroutine.yield regularly in the function.
And, that loop should respect the filters that coroutine.yeild returns, and properly handle an error, and take proper action when the coroutine completes successfully.
Posted 15 March 2016 - 07:09 PM
Guys, I said the setfenv was throwing me off
I wish there was some way to close the topic…
I wish there was some way to close the topic…
Posted 15 March 2016 - 07:32 PM
Don't you understand setfenv? It is basically a way to associate a table to a function. Whenever a value is requested that is not local, Lua will look in the environment. This is why it's common practice to set the env to _G: setfenv(func, _G)
Posted 15 March 2016 - 09:39 PM
I figured that out eventualy. I still don't like getfenv.
Posted 15 March 2016 - 10:35 PM
Why?I figured that out eventualy. I still don't like getfenv.
Posted 15 March 2016 - 11:42 PM
I have a file that's supposed to manage files coroutines. Yet when I set running to true, run openTab,and then run
runtabs, it only runs the very basic files. Why?
runtabs, it only runs the very basic files. Why?
window.ID=0
function _G.window.id()
return window.ID;
end
shell.running={}
shell.openTab = function(file)
f=loadfile(file)
table.insert(shell.running, coroutine.create(f))
end;
shell.runtabs = function()
while running do
window.id=0
for n, task in ipairs(shell.running) do
window.ID=window.ID+1
if running then
if task ~= nil then
if coroutine.status(task) ~= 'dead' then
coroutine.resume(task);
end
end
end
end
end
end
Edited on 15 March 2016 - 10:46 PM
Posted 16 March 2016 - 12:17 AM
Check out Bomb Bloke's coroutine tutorial, specifically chapter four.
Posted 16 March 2016 - 12:26 AM
Well, maybe you'll get away with starting there. You'll want to go through five as well.
Posted 16 March 2016 - 01:53 PM
I haven't implemented yield yet. It's not that.
Posted 16 March 2016 - 03:42 PM
I haven't implemented yield yet. It's not that.
This sentence tells me you have no idea how coroutines work.
Spoiler
how the heck does spellcheck not mark sentance as wrong????Edited on 16 March 2016 - 03:03 PM
Posted 16 March 2016 - 03:50 PM
Even os.pullEventRaw is only a wrapper for coroutine.yield
Also, sentence, not sentance.
Also, sentence, not sentance.
Posted 16 March 2016 - 04:05 PM
I haven't implemented yield yet. It's not that.
This sentence tells me you have no idea how coroutines work.
[anti-spoiler]
how the heck does spellcheck not mark sentance as wrong????
[/anti-spoiler]
Have you never heard of coroutine.yield?
Also, get Grammarly… [attachment=2521:Screenshot 2016-03-16 at 12.07.35 PM.png]
https://chrome.googl...obkghlhen?hl=en
Edited on 25 May 2016 - 05:19 PM
Posted 16 March 2016 - 06:07 PM
You're forced to yield, if you don't yield at all you're going to have major problems.
Posted 16 March 2016 - 07:08 PM
As you know from my previous post, http://www.computercraft.info/forums2/index.php?/topic/26223-file-coroutine-management-problems/, my attempt to do this didn't go so well. Does anyone know of a multitasking coroutine manager that has the following features:
*Must run the coroutines from a list rather than a variable
*Must only run coroutine if it's actually a coroutine
*Must not run dead coroutines
*Must run the coroutines from a list rather than a variable
*Must only run coroutine if it's actually a coroutine
*Must not run dead coroutines
Posted 16 March 2016 - 07:18 PM
I provided you with a link in one of the other threads. It also has filtering. Feel free to use it.
Else, it is here.
Else, it is here.
Posted 16 March 2016 - 07:21 PM
I'll try itI provided you with a link in one of the other threads. It also has filtering. Feel free to use it.
Else, it is here.
I specifically have hopes for thread.run
But why's it using an event to resume it?
Edited on 16 March 2016 - 06:23 PM
Posted 16 March 2016 - 07:32 PM
Because that's kinda how CC runs. Even the manager is required to yield to the overall Lua vm that runs each separate computer as it's own coroutine.
Posted 16 March 2016 - 07:35 PM
I will make a slight edit for my useI provided you with a link in one of the other threads. It also has filtering. Feel free to use it.
Else, it is here.
_G.thread={}
thread.run(t, func)
local event= {}
while running do
event ={os.pullEvent()}
for i,v ini pairs(t) do
if running then
v.resume unpack(event))
end
end
if func then
func()
end
end
end
Should I give credit?Edited on 16 March 2016 - 07:01 PM
Posted 16 March 2016 - 07:57 PM
Yep you should. Look at the for loop ==> in not ini and if func then
Posted 16 March 2016 - 08:25 PM
I know what coroutine.yeild is. I know how to use coroutines. You do not implement coroutine.yeild, you have to make your manager run around the rules CC has already set for coroutine.yeild / resume interaction (hint: it involves events).
Posted 16 March 2016 - 09:39 PM
yield.
Posted 16 March 2016 - 09:59 PM
Either way, you need to yield for any CC program including coroutine managers
Posted 16 March 2016 - 11:24 PM
everyOS, please stick to the one thread for your coroutine queries. There's no need to start a new one every day.
Posted 17 March 2016 - 11:54 AM
You do know one of the other threads had a link I needed, right. You could have tooken that into consideration before deleting the other topics.everyOS, please stick to the one thread for your coroutine queries. There's no need to start a new one every day.
Edit: Never mind. You merged them all. Just now noticed
Edited on 17 March 2016 - 10:56 AM