Difference between revisions of "Parallel.waitForAny"

From ComputerCraft Wiki
Jump to: navigation, search
(Some attempts at clarification.)
(Removed very misleading incorrect note)
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
 
{{Function
 
{{Function
 
|name=parallel.waitForAny
 
|name=parallel.waitForAny
|args= function1, function2, so on
+
|args={{type|function}} function1, {{type|function}} function2, ...
|returns = A number indicating which function completed based on argument order
+
|returns = {{type|number}} indicating which function has completed based on argument order
 
|api=parallel
 
|api=parallel
 
|addon=ComputerCraft
 
|addon=ComputerCraft
|desc=Lets you run multiple functions at the same time, alternating between them until any happen to finish. Note that it takes ''function names'' as parameters (eg "function1", "function2", etc), as opposed to ''function calls'' (eg "function1()", "function2()", etc).<br><br>
+
|desc=Lets you run multiple functions at the same time, alternating between them until any happen to finish.
 
+
The functions are not actually executed simultaniously, but rather this API will automatically switch between them whenever they yield (eg whenever they call [[Coroutine.yield|coroutine.yield()]], or functions that call that - eg [[Os.pullEvent|os.pullEvent()]] - or functions that call ''that'', etc - basically, anything that causes the function to "pause").<br><br>
+
 
+
Each function executed in "parallel" gets its own copy of the event queue, and so "event consuming" functions (again, most anything that causes the script to pause - eg [[Os.sleep|sleep()]], [[Rednet.receive|rednet.receive()]], most of the [[Turtle_(API)|turtle API]], etc) can safely be used in one without affecting the event queue accessed by the other.
+
 
|examples=
 
|examples=
 
{{Example
 
{{Example
|desc=Tells the computer to run function1 and function2 at the same time. Will broadcast what the user enters or prints what it receives over rednet, depending on what happens first.
+
|desc=Tells the computer to run <var>receive</var >and <var>send</var> the same time. Will broadcast what the user enters or prints what it receives over rednet, depending on what happens first.
|code=function1 = [[Function (type)|function]]()  -- Define function1.
+
|code= local function receive ()  -- Define function1.
   [[print]] ([[rednet.receive]]())
+
   [[print]]([[rednet.receive]]())
 
  end
 
  end
 
   
 
   
  [[Function (type)|function]] function2()  -- Define function2.
+
  local function send ()  -- Define function2.
   [[rednet.broadcast]] ([[read]]())
+
   [[rednet.broadcast]]([[read]]())
 
  end
 
  end
 
   
 
   
  parallel.waitForAny (function1, function2)  -- Run both functions in parallel until one finishes.
+
  '''parallel.waitForAny(receive, send)''' -- Run both functions in parallel until one finishes.
 
|output=Either it prints what it received via rednet, or it broadcasts the message the user typed.
 
|output=Either it prints what it received via rednet, or it broadcasts the message the user typed.
 
}}
 
}}

Latest revision as of 21:54, 27 January 2016

Grid Redstone.png  Function parallel.waitForAny
Lets you run multiple functions at the same time, alternating between them until any happen to finish.
Syntax parallel.waitForAny(function function1, function function2, ...)
Returns number indicating which function has completed based on argument order
Part of ComputerCraft
API parallel

Examples

Grid paper.png  Example
Tells the computer to run receiveand send the same time. Will broadcast what the user enters or prints what it receives over rednet, depending on what happens first.
Code
local function receive ()  -- Define function1.
 print(rednet.receive())
end

local function send ()  -- Define function2.
 rednet.broadcast(read())
end

parallel.waitForAny(receive, send)  -- Run both functions in parallel until one finishes.
Output Either it prints what it received via rednet, or it broadcasts the message the user typed.