### Child Process Communication with IPC (JavaScript) Source: https://github.com/tinylibs/tinypool/blob/main/README.md Demonstrates inter-process communication (IPC) between the main process and worker processes managed by Tinypool. This example shows how to send messages from the main process to the worker and receive responses, using a custom channel object. ```javascript // main.mjs import Tinypool from 'tinypool' const pool = new Tinypool({ runtime: 'child_process', filename: new URL('./worker.mjs', import.meta.url).href, }) const messages = [] const listeners = [] const channel = { onMessage: (listener) => listeners.push(listener), postMessage: (message) => messages.push(message), } const promise = pool.run({}, { channel }) // Send message to worker setTimeout( () => listeners.forEach((listener) => listener('Hello from main process')), 1000 ) // Wait for task to finish await promise console.log(messages) // [{ received: 'Hello from main process', response: 'Hello from worker' }] ``` ```javascript // worker.mjs export default async function run() { return new Promise((resolve) => { process.on('message', (message) => { // Ignore Tinypool's internal messages if (message?.__tinypool_worker_message__) return process.send({ received: message, response: 'Hello from worker' }) resolve() }) }) } ``` -------------------------------- ### Basic Child Process Pool Usage (JavaScript) Source: https://github.com/tinylibs/tinypool/blob/main/README.md Shows the basic usage of Tinypool when utilizing the `child_process` runtime. This allows running tasks in separate processes instead of threads. The setup is similar to worker threads, but specifies the `runtime` option. ```javascript // main.mjs import Tinypool from 'tinypool' const pool = new Tinypool({ runtime: 'child_process', filename: new URL('./worker.mjs', import.meta.url).href, }) const result = await pool.run({ a: 4, b: 6 }) console.log(result) // Prints 10 ``` ```javascript // worker.mjs export default ({ a, b }) => { return a + b } ``` -------------------------------- ### Worker Thread Communication with MessageChannel (JavaScript) Source: https://github.com/tinylibs/tinypool/blob/main/README.md Illustrates how to establish communication between the main thread and worker threads using `MessageChannel` with Tinypool. The main thread sends data to the worker via a shared port, and the worker responds. This example involves setting up ports and handling messages on both ends. ```javascript // main.mjs import Tinypool from 'tinypool' import { MessageChannel } from 'node:worker_threads' const pool = new Tinypool({ filename: new URL('./worker.mjs', import.meta.url).href, }) const { port1, port2 } = new MessageChannel() const promise = pool.run({ port: port1 }, { transferList: [port1] }) port2.on('message', (message) => console.log('Main thread received:', message)) setTimeout(() => port2.postMessage('Hello from main thread!'), 1000) await promise port1.close() port2.close() ``` ```javascript // worker.mjs export default ({ port }) => { return new Promise((resolve) => { port.on('message', (message) => { console.log('Worker received:', message) port.postMessage('Hello from worker thread!') resolve() }) }) } ``` -------------------------------- ### Basic Worker Thread Pool Usage (JavaScript) Source: https://github.com/tinylibs/tinypool/blob/main/README.md Demonstrates the basic usage of Tinypool with Node.js worker threads. It initializes a pool, runs a task defined in a separate worker file, and then destroys the pool. This example requires two files: `main.mjs` for the main thread and `worker.mjs` for the worker logic. ```javascript // main.mjs import Tinypool from 'tinypool' const pool = new Tinypool({ filename: new URL('./worker.mjs', import.meta.url).href, }) const result = await pool.run({ a: 4, b: 6 }) console.log(result) // Prints 10 // Make sure to destroy pool once it's not needed anymore // This terminates all pool's idle workers await pool.destroy() ``` ```javascript // worker.mjs export default ({ a, b }) => { return a + b } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.