Our example demonstrates how to implement a worker pool using PureScript’s asynchronous capabilities.
In this PureScript version, we use the Aff monad for asynchronous operations. The worker function simulates work with a delay, and createWorker processes multiple jobs for a single worker.
We create three workers, each handling a subset of the jobs. The parallel and sequential functions from Control.Parallel are used to run the workers concurrently.
To run the program, you would typically use the PureScript compiler (purs) and Node.js:
The program shows the 5 jobs being executed by various workers. It takes about 2 seconds to complete despite doing about 5 seconds of total work because there are 3 workers operating concurrently.
Note that PureScript’s approach to concurrency is different from traditional multithreading. It uses asynchronous operations and the Aff monad to handle concurrent tasks, which is more similar to JavaScript’s event loop model than to OS-level threads.