local width, height = term.getSize() local halfwidth = math.floor(width/2) local window1 = window.create(term.current(), 1, 1, halfwidth, height) local window2 = window.create(term.current(), halfwidth, 1, halfwidth, height) parallel.waitForAll( function() --# I kow that this is only called once term.redirect(window1) shell.run("worm") end, function() --# I kow that this is only called once term.redirect(window2) shell.run("paint image") end )How can I manage to redirect the terminal in time so the right program can use it?
use parallel api to run multiple programs on one screen
#1
Posted 08 April 2018 - 06:54 PM
#2
Posted 08 April 2018 - 07:01 PM
- Redirect to window1, run worm, worm yields
- Redirect to window2, run paint, paint yields
- Wait for an event
- Resume worm with this event, worm yields
- Resume paint with this event, paint yields,
- etc...
I'd recommend having a look at how multishell handles this, as well as the implementation of the parallel API. CraftOS's code isn't always the cleanest, but it's often a good starting point on how to implement features like this.
#3
Posted 08 April 2018 - 07:07 PM
SquidDev, on 08 April 2018 - 07:01 PM, said:
- Redirect to window1, run worm, worm yields
- Redirect to window2, run paint, paint yields
- Wait for an event
- Resume worm with this event, worm yields
- Resume paint with this event, paint yields,
- etc...
SquidDev, on 08 April 2018 - 07:01 PM, said:
I'd recommend having a look at how multishell handles this, as well as the implementation of the parallel API. CraftOS's code isn't always the cleanest, but it's often a good starting point on how to implement features like this.
Thanks btw for the quick reply! It was very helpful.
#4
Posted 08 April 2018 - 07:23 PM
#5
Posted 08 April 2018 - 07:26 PM
Jummit, on 08 April 2018 - 07:23 PM, said:
#6
Posted 08 April 2018 - 07:29 PM
SquidDev, on 08 April 2018 - 07:26 PM, said:
Jummit, on 08 April 2018 - 07:23 PM, said:
#7
Posted 08 April 2018 - 08:07 PM
#8
Posted 08 April 2018 - 08:14 PM
Purple, on 08 April 2018 - 08:07 PM, said:
*It's worth noting that PUC Lua doesn't even use threads at all - a yield is little more than a jump into the parent coroutine's function. LuaJ does use threads, but that's an implementation detail one does not need to worry about.
For instance, consider the following Lua code:
local count = 0 parallel.waitForAll(function() while true do os.pullEvent("test") -- Yields here --# If Lua used preemptive multi-threading, this has the potential to be a race condition. However, + will not yield --# so it's fine. count = count + 1 end end, function() while true do sleep(math.random(1, 5)) --# Yields here count = count + 1 --# Allows the above function to continue (due to test event). Note it won't actually start running until we're processing the --# "test" event, which may be several yields later. os.queueEvent("test") end end)
Edited by SquidDev, 08 April 2018 - 08:21 PM.
#9
Posted 09 April 2018 - 07:15 AM
local width, height = term.getSize() local halfwidth = math.floor(width/2) local window1 = window.create(term.current(), 1, 1, halfwidth, height) local window2 = window.create(term.current(), halfwidth, 1, halfwidth, height) local worm = coroutine.create(function() shell.run("worm") end) local paint = coroutine.create(function() shell.run("paint test") end) while true do term.redirect(window1) coroutine.resume(worm) term.redirect(window2) coroutine.resume(paint) end
I get the typical too long without yielding error. What am I doing wrong here?
#10
Posted 09 April 2018 - 08:29 AM
local co1 = coroutine.create(function() for i = 1, 10 do print("toast"..i) sleep(0.5) end end) local co2 = coroutine.create(function() for i = 1, 10 do print("test"..i) sleep(0.5) end end) local resume = function(co) local status = coroutine.status(co) if status == "normal" or status == "suspended" then coroutine.resume(co) print("resuming coroutine with status "..status) else print("not resuming coroutine with status "..status) end end while true do resume(co1) resume(co2) end
Edited by Jummit, 09 April 2018 - 08:35 AM.
#11
Posted 09 April 2018 - 06:22 PM
Edited by Lupus590, 09 April 2018 - 06:22 PM.
#13
Posted 09 April 2018 - 06:45 PM
Lupus590, on 09 April 2018 - 06:22 PM, said:
local co1 = coroutine.create(function() for i = 1, 10 do print("toast"..i) sleep(0.5) coroutine.yield() end end) local co2 = coroutine.create(function() for i = 1, 10 do print("test"..i) sleep(0.5) coroutine.yield() end end) local resume = function(co) local status = coroutine.status(co) if status == "normal" or status == "suspended" then coroutine.resume(co) end end while true do resume(co1) resume(co2) end
Edited by Jummit, 09 April 2018 - 06:46 PM.
#14
Posted 09 April 2018 - 07:11 PM
#16
Posted 09 April 2018 - 07:54 PM
#17
Posted 10 April 2018 - 07:27 AM
I managed to get it to work:
local width, height = term.getSize() local halfwidth = math.floor(width/2) local window1 = window.create(term.current(), 1, 1, halfwidth, height) local window2 = window.create(term.current(), halfwidth, 1, halfwidth, height) local co1 = coroutine.create(function() os.run(_G, "rom/programs/fun/worm.lua") end) local co2 = coroutine.create(function() os.run(_G, "rom/programs/edit.lua", "test") end) while true do term.redirect(window1) coroutine.resume(co1, os.pullEventRaw()) term.redirect(window2) coroutine.resume(co2, os.pullEventRaw()) endBut edit, paint and redirection always give the error 'loop in gettable'. What does this mean? The error comes from the shell api, but I don't know what happens in there.
EDIT: this explains everything
Edited code:
local width, height = term.getSize() local halfwidth = math.floor(width/2) local window1 = window.create(term.current(), 1, 1, halfwidth, height) local window2 = window.create(term.current(), halfwidth, 1, halfwidth, height) local co1 = coroutine.create(function() shell.run("rom/programs/fun/worm.lua") end) local co2 = coroutine.create(function() shell.run("rom/programs/edit.lua", "test") end) while true do local eventData = {os.pullEventRaw()} term.redirect(window1) coroutine.resume(co1, table.unpack(eventData)) term.redirect(window2) coroutine.resume(co2, table.unpack(eventData)) end
Edited by Jummit, 10 April 2018 - 09:15 AM.
#18
Posted 10 April 2018 - 10:20 AM
SquidDev, on 08 April 2018 - 08:14 PM, said:
I am of course operating from the perspective of writing each program individually and not wanting to care about what else they will be running along side with as I do so.
#19
Posted 10 April 2018 - 06:46 PM
Purple, on 10 April 2018 - 10:20 AM, said:
Use local variables?
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users