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

parallel.waitForAll getting stuck

Started by Larethian, 18 July 2014 - 10:43 PM
Larethian #1
Posted 19 July 2014 - 12:43 AM
Hi together,

I'm currently in trying to automate a smeltery with my programm, and to speed things up a bit I tried to have block, ingot and nugget-creation all at the same time by one computer.

First: parallel.waitForAll() does not execute the functions parallel, but sequential in order of appearance, means blocks first, then ingots, then nuggets.

Second: parallel.waitForAll() executes the three create*()-functions without any problems, but will wait forever after the last function ran through. print("Task completed") is never called on my two testcomputers, and I have no idea why.

I would appreciate every idea how to solve this without getting to complex. A solution I thought of would be to use coroutines on my own, but this wouldn't explain why parallel.waitForAll() is not working and I would stumble over it in my next project again without a solution.

Code:

local blockTime = 20
local ingotTime = 7
local nuggetTime = 5
local blockSide = "left"
local ingotSide = "right"
local nuggetSide = "top"
----------------------------------------------
local function printUsage()
  term.clear()
  term.setCursorPos(1,1)
  print("Provide 2 or 3 numbers!")
end
local function createBlocks(blocks)
  local i = 0
  while i < blocks do
	i = i+1
	print("Started Block")
	rs.setOutput(blockSide, true)
	sleep(0.5)
	rs.setOutput(blockSide, false)
	sleep(blockTime)
	print("Finished Block")
  end
end
local function createIngots(ingots)
  local i = 0
  while i < ingots do
	i = i+1
	print("Started Ingot")
	rs.setOutput(ingotSide, true)
	sleep(0.5)
	rs.setOutput(ingotSide, false)
	sleep(ingotTime)
	print("Finished Ingot")
  end
end
local function createNuggets(nuggets)
  local i = 0
  while i < nuggets do
	i = i+1
	print("Started Nugget")
	rs.setOutput(nuggetSide, true)
	sleep(0.5)
	rs.setOutput(nuggetSide, false)
	sleep(nuggetTime)
	print("Finished Nugget")
  end
end
----------------------------------------------
local args = {...}
if table.getn(args) ~= 2 and table.getn(args) ~= 3 then
  printUsage()
  return
end
if tonumber(args[1]) == nil or tonumber(args[2]) == nil then
  printUsage()
  return
end
if table.getn(args) == 3 and tonumber(args[3]) == nil then
  printUsage()
  return
end
local blocks = tonumber(args[1])
local ingots = tonumber(args[2])
local nuggets = table.getn(args) == 3 and tonumber(args[3]) or 0
print("Task started")
print("Will create "..blocks .." blocks, "..ingots.." ingots and "..nuggets.." nuggets")
parallel.waitForAll(createBlocks(blocks), createIngots(ingots), createNuggets(nuggets))			-- THIS parallel.waitForAll()
print("Task Finished")
----------------------------------------------
Lyqyd #2
Posted 19 July 2014 - 01:05 AM
You're using the parallel API incorrectly. You're calling the functions and passing the results of them to the parallel API, instead of passing the functions themselves. Since you're also passing arguments, you'll want to simply create anonymous functions. Replace the line with:


parallel.waitForAll(function() createBlocks(blocks) end, function() createIngots(ingots) end, function() createNuggets(nuggets) end)