Package Exports
- twerker
- twerker/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (twerker) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Twerker
TypeScript Worker Threads Made Simple
A lightweight library for running TypeScript functions in worker threads with type safety and a simple API.
Features
- ๐ Type Safe: Full TypeScript support with automatic type inference for worker functions
- ๐ Simple API: Run any function in a worker thread with minimal setup
- ๐งต Thread Pools: Create worker pools for processing multiple tasks in parallel
- ๐ Asynchronous: Promise-based API for seamless integration
- ๐งฉ Preserves Context: Your functions run with proper access to imports and dependencies
- ๐ Clean Exit: Worker pools terminate properly when no longer needed
Installation
npm install twerker
Quick Start
import run from 'twerker';
// Define a function to run in a worker thread
function expensiveCalculation(n: number): number {
let result = 0;
for (let i = 0; i < n; i++) {
result += Math.sqrt(i);
}
return result;
}
async function main() {
// Create a worker
const worker = run(expensiveCalculation);
// Execute the function in a worker thread
const result = await worker.execute(1000000);
console.log('Result:', result);
// Create a worker pool with 4 workers
const pool = worker.createPool({ numWorkers: 4 });
// Queue multiple tasks
const results = await Promise.all([
pool.queue(1000000),
pool.queue(2000000),
pool.queue(3000000),
pool.queue(4000000)
]);
console.log('Results:', results);
// Terminate the pool when done
await pool.terminateWhenDone();
}
main().catch(console.error);
API Reference
run<T>(workerFunction: T, options?: TwerkerOptions): Worker<T>
Creates a worker from a TypeScript function.
const worker = run(myFunction, {
resourceLimits: { maxOldGenerationSizeMb: 256 }
});
Worker
The Worker
object returned by the run
function.
execute(...args)
: Execute the worker function with the provided argumentscreatePool(options)
: Create a worker pool for processing multiple tasksunref()
: Unref the worker from the Node.js event loop (allows program to exit naturally)
WorkerPool
A pool of workers for processing multiple tasks in parallel.
queue(...args)
: Queue a task to be processed by an available workerwaitForAllTasks()
: Wait for all queued and running tasks to completeterminateWhenDone()
: Terminate all workers after all tasks are completedterminate()
: Terminate all workers immediately (cancels pending tasks)unref()
: Unref the worker pool from the Node.js event loop
Memory and Resource Management
When working with worker threads, it's important to properly manage resources:
- Always call
terminateWhenDone()
when you're done with a worker pool - Use
unref()
when you want the program to exit naturally even if workers are still running - Set resource limits if needed using the
resourceLimits
option
License
ISC