nexuslua
|
The addagent function in nexuslua offers a mechanism to spawn a new operating system thread. Once created, this thread awaits messages, executing the provided Lua code whenever a message is sent to this agent via the send function.
The function takes in two distinct parameters and an optional third:
Consider this example where an agent named "numbers" is created using code from an external file, numbers.lua:
The numbers.lua file might contain functions like IsPrime, as demonstrated in this snippet:
With the agent now active, you can send messages to it, prompting the execution of the IsPrime function in an asynchronous fashion. For a hands-on demonstration of this process, please refer to the send documentation.
The last line addmessage("IsPrime") registers this message name to the agent, so it accepts messages of this name and calls the equally named function with all parameters that the sender adds to the send command, for example
(See send for an example on how to evaluate the result of IsPrime.)
As syntactic sugar, addagent accepts an optional third parameter with a list of messages to register:
In this case, the agent itself does not need to call addmessage as in the example above.
It's essential to comprehend that addagent isn't merely about defining functions but setting up independent execution threads. These agents operate in parallel, responding to messages sent to them and thus enable concurrent processing in your nexuslua scripts.
When executing the command line interface of nexuslua, the provided script is executed as an agent with name "main".
As before, the focus has been on clarity and breaking down the information into easily digestible chunks. The segment titled "Key Insights" is an optional addition to give users a quick conceptual overview of the function's significance. If it seems out of place or unnecessary for your documentation style, feel free to exclude it.