This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Kamaroth's profile picture

Dynamically updating the functions that parallel.waitForAny uses.

Started by Kamaroth, 21 July 2013 - 06:02 AM
Kamaroth #1
Posted 21 July 2013 - 08:02 AM
Title: Dynamically updating the functions that parallel.waitForAny uses.

Hi. I'm writing a program which uses parallel.waitForAny to sit and wait for a rednet message from a server. Generally this message will contain a set of parameters for a task which the turtle passes to a function of it's own. For example, a set of coordinates for mining. By default I just use:

parallel.waitForAny(receive)

However when the other function is called I need the turtle to still be able to listen and respond to rednet messages. Is there any way to dynamically add another function so the end result of the parallel command is something like:

parallel.waitForAny(receive, parseCommand(command,param))

I thought about having a string of the parallel command I need to run but I couldn't get that to work. It seems like it would run the command, but the turtle would be unresponsive. For the record the code I tried to use to get that to work was:

local parallelCommand = "parallel.waitForAny(recieve)"

...

assert(loadstring(parallelCommand))()
Lyqyd #2
Posted 21 July 2013 - 04:14 PM
Split into new topic.

You can't do it the way you're trying to do it. Instead, use a second function that pulls the commands out of a table. Put the functions/strings/etc. you want it to run into the table from the coroutine you currently have, then queue an event to tell the other coroutine that there is something in the table. Have the execution coroutine wait on those events.
Yevano #3
Posted 21 July 2013 - 05:20 PM
Alternatively, you could use an API which I have created to dynamically add coroutines to a list of threads to run in parallel. I made it for an old project of mine, and you can find it here. I have documentation of it in this post. Just search for shared/ThreadPool in that topic.
Lyqyd #4
Posted 21 July 2013 - 05:26 PM
If existing software solutions are desired, GopherAtl's goroutines are more fully-featured.
Kamaroth #5
Posted 21 July 2013 - 11:14 PM
Thanks Yevano and Lyqyd. It appears goroutines looks like it would be perfect for what I need.