Goroutines in Wolfram Language
On this page
Wolfram Language: Goroutines Example
A goroutine is a lightweight thread of execution. In Wolfram Language, we use parallel computing constructs to achieve similar concurrent execution.
Here’s how you can achieve similar functionality using Wolfram Language.
In the above code:
- We define a function
f
that takes a stringfrom
and prints it with an index three times. - We call
f("direct")
synchronously. - We use Wolfram Language’s
ParallelSubmit
to create tasks that runf("goroutine")
andPrint["going"]
concurrently. WaitAll
is used to wait for these tasks to complete.- Finally, we print “done”.
Explanation:
To run this code, you just need to evaluate the cell in a Wolfram Language environment (like Mathematica or Wolfram Cloud).
When you run the program, you will see the output of the synchronous call first, followed by the output of the asynchronous tasks. The outputs of the tasks may interleave as they are executed concurrently by the Wolfram Language kernel.
Expected Output:
When you run this program, you should see output similar to the following:
In Wolfram Language, task outputs can be interleaved because tasks are run concurrently by the parallel computation capabilities of the language.