Parallel API

From ComputerCraft
Jump to: navigation, search
FunctionFunctionparallel.waitForAll
Switches between execution of the functions, until all of them are finished. If any of the functions errors, the message is propagated upwards from the parallel.waitForAll call.[1]
Syntax parallel.waitForAll()
Returns nil
Part of CC:Tweaked (source)
API parallel
See also parallel.waitForAny
ExampleExampleDownload a file while reading the file name
The file download and read for the file name will execute in parallel, taking turns until both return. Since both read and http.get yield regularly (the first one waiting for characters, and the second waiting for a response), no deadlocking occurs.
Code
local handle, file
parallel.waitForAll(
  function()
    io.write("enter a file name: ")
    file = fs.open(read(), "w")
  end,
  function() handle = http.get("https://example.computercraft.cc") end
)
file.writeLine(handle.readAll())
handle.close()
file.close()
Output Writes the contents of example.computercraft.cc to the filename that the user entered.


FunctionFunctionparallel.waitForAny
Switches between execution of the functions, until any of them finishes. If any of the functions errors, the message is propagated upwards from the parallel.waitForAny call.[2]
Syntax parallel.waitForAny()
Returns nil
Part of CC:Tweaked (source)
API parallel
See also parallel.waitForAll
ExampleExampleWait for user interaction or a timeout
Do nothing until the user presses a key or 10 seconds have elapsed.
Code
parallel.waitForAny(
  function()
    local ev
    repeat
      ev = coroutine.yield()
    until ev == 'key'
  end,
  function() sleep(10) end
)
print("Goodbye!")
Output Prints Goodbye! after either 10 seconds or the user presses a key.

References

  1. "CC-Tweaked/parallel.lua". GitHub. 2017-06-12. Retrieved 2018-08-06. 
  2. "CC-Tweaked/parallel.lua". GitHub. 2017-06-12. Retrieved 2018-08-06.