Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

IguanaX components operate on a single thread or within a single Lua instance. This means that when a message is received by a component, it must complete the full execution of the main() function before the next message can be processed.

For workflows that require handling multiple simultaneous tasks, such as a multi-threaded web server managing concurrent connections, parallel processing is a powerful technique to improve efficiency and scalability. This approach is only suitable for when the order in which messages are processed is not important.

In IguanaX, parallel processing within components is achieved using the component.clone method to create multiple Lua instances (or threads) for processing. Messages can then be distributed across a pool of threads with component.call. Each thread processes messages independently, allowing for faster performance in high-throughput scenarios.

Let’s take a look at a simple example:

 STEP 1: Add two custom components to your Iguana, name one "Dispatcher" and the other "Worker"

 STEP 3: Worker

Typically, the component.clone call would be a part of the initialization logic so that when the component is turned on, multiple Lua threads are spawned. After this, whenever the component receives a message, the first available Lua thread will take it and process it according to the script.

component.clone has a maximum thread count of 100. You must provide the count parameter a number between 2 and 100.

function processMessage(Data)
   -- Data comes in as a json string, parse to get the parameters
   local ParsedData = json.parse{data=Data}
   local TaskId = ParsedData.id
   local Payload = ParsedData.payload
   -- process data 
end

function main(Data)
   if Data == "INIT" then
      -- Make 10 threads
      component.clone{count=10}
      return
   end
   processMessage(Data)
end
 STEP 3: Dispatcher

In the dispatcher, the data is prepared and component.call is used to call the main function within the Worker component.

For component.call to work, the target component must be running.

function main(Data)
   if Data == "INIT" then
      return
   end
   
   -- Create sample JSON to dispatch
   local CallData = {}
   CallData.id = 123
   CallData.payload = Data
   CallData = json.serialize{data=CallData}
   
   -- This will call the main function of Worker and immediately return
   component.call{name="Worker", data=CallData, func="main", async=true}
end

link Call a function within another Component

Multithreaded web service

  • No labels