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

Run program as coroutine

Started by EveryOS, 14 March 2016 - 12:57 PM
EveryOS #1
Posted 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.
EveryOS #2
Posted 14 March 2016 - 02:15 PM
Actually, is this possible. Would it work to just call my terminal program, or what?
Lupus590 #3
Posted 14 March 2016 - 02:47 PM
try this: http://www.computercraft.info/forums2/index.php?/topic/25670-bbs-guide-to-coroutines/
EveryOS #4
Posted 14 March 2016 - 04:34 PM
That didn't help…
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
KingofGamesYami #5
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.
EveryOS #6
Posted 14 March 2016 - 06:05 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.
Nah…
tball146 #7
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')
EveryOS #8
Posted 14 March 2016 - 06:40 PM
I need to run a program as a coroutine
Not a function!!
Lyqyd #9
Posted 14 March 2016 - 06:41 PM
Check out os.run for an example of loading a program up into a function for use.
EveryOS #10
Posted 14 March 2016 - 07:02 PM
Check out os.run for an example of loading a program up into a function for use.
Didn't work
–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
Lupus590 #11
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
EveryOS #12
Posted 14 March 2016 - 07:07 PM
There's an apis file for the os?
EveryOS #13
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
Creator #14
Posted 14 March 2016 - 07:39 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
EveryOS #15
Posted 14 March 2016 - 07:52 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
Yaay! (:=
Creator #16
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
EveryOS #17
Posted 14 March 2016 - 08:00 PM
So, I helped you? Or was that a scream of despair? :P/>
I'm waiting to test it, but I think it'll work
Lupus590 #18
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.
EveryOS #19
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
EveryOS #20
Posted 15 March 2016 - 04:36 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
Never mind. SetFenv threw me off
Creator #21
Posted 15 March 2016 - 06:08 PM
And, there should be a loop that calls coroutine.yield regularly in the function.
KingofGamesYami #22
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.
Creator #23
Posted 15 March 2016 - 06:53 PM
Here is some code I wrote some time ago that might help you.
EveryOS #24
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…
Creator #25
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)
EveryOS #26
Posted 15 March 2016 - 09:39 PM
I figured that out eventualy. I still don't like getfenv.
H4X0RZ #27
Posted 15 March 2016 - 10:35 PM
I figured that out eventualy. I still don't like getfenv.
Why?
EveryOS #28
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?

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
Lyqyd #29
Posted 16 March 2016 - 12:17 AM
Check out Bomb Bloke's coroutine tutorial, specifically chapter four.
Bomb Bloke #30
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.
EveryOS #31
Posted 16 March 2016 - 01:53 PM
I haven't implemented yield yet. It's not that.
KingofGamesYami #32
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.

Spoilerhow the heck does spellcheck not mark sentance as wrong????
Edited on 16 March 2016 - 03:03 PM
Creator #33
Posted 16 March 2016 - 03:50 PM
Even os.pullEventRaw is only a wrapper for coroutine.yield

Also, sentence, not sentance.
EveryOS #34
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
Dragon53535 #35
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.
EveryOS #36
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
Creator #37
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.
EveryOS #38
Posted 16 March 2016 - 07:21 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.
I'll try it
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
Dragon53535 #39
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.
EveryOS #40
Posted 16 March 2016 - 07:35 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.
I will make a slight edit for my use

_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
Creator #41
Posted 16 March 2016 - 07:57 PM
Yep you should. Look at the for loop ==> in not ini and if func then
KingofGamesYami #42
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).
FUNCTION MAN! #43
Posted 16 March 2016 - 09:39 PM
yield.
Dragon53535 #44
Posted 16 March 2016 - 09:59 PM
Either way, you need to yield for any CC program including coroutine managers
Bomb Bloke #45
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.
EveryOS #46
Posted 17 March 2016 - 11:54 AM
everyOS, please stick to the one thread for your coroutine queries. There's no need to start a new one every day.
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.

Edit: Never mind. You merged them all. Just now noticed
Edited on 17 March 2016 - 10:56 AM