Parallel API

Functionparallel.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(
  • functions... : function
)
Returns nil
Part of CC:Tweaked (source)
API parallel
See also parallel.waitForAny
ExampleDownload 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
<nowiki>
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()
</nowiki>
Output Writes the contents of example.computercraft.cc to the filename that the user entered.
Functionparallel.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(
  • functions... : function
)
Returns nil
Part of CC:Tweaked (source)
API parallel
See also parallel.waitForAll
ExampleWait for user interaction or a timeout
Do nothing until the user presses a key or 10 seconds have elapsed.
Code
<nowiki>
parallel.waitForAny(
  function()
    local ev
    repeat
      ev = coroutine.yield()
    until ev == 'key'
  end,
  function() sleep(10) end
)
print("Goodbye!")
</nowiki>
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.
This article is issued from Computercraft. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.