Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Snippet: Native node:worker_threads example ts/esm #10

Open
trasherdk opened this issue Jul 31, 2022 · 2 comments
Open

Snippet: Native node:worker_threads example ts/esm #10

trasherdk opened this issue Jul 31, 2022 · 2 comments

Comments

@trasherdk
Copy link
Owner

index.ts

import { Worker } from 'node:worker_threads'

const worker = new Worker(new URL('./worker.ts', import.meta.url), {
	workerData: [2, 2],
	execArgv: ['--loader', 'ts-node/esm'],
})

const result = await new Promise<number>((resolve, reject) => {
	worker.on('message', resolve)
	worker.on('error', reject)
	worker.on('exit', (code) => {
		if (code !== 0) {
			reject(new Error(`Worker stopped with exit code ${code}`))
		}
	})
})

console.log(result)

worker.ts

import { parentPort, workerData } from 'node:worker_threads'

const [a, b] = workerData

parentPort?.postMessage(a + b)

Source: https://github.com/lynxtaa/threads-ts-node-esm-issue/tree/no-threads

@trasherdk
Copy link
Owner Author

Expanded version:

I've omitted a threads.js dependency long time ago and wrote a simple helper for creating workers, which works in compiled code and in tests. Hope someone finds it helpful.

// runWorker.ts
import { cpus } from 'os'
import { Worker } from 'worker_threads'

import PQueue from 'p-queue'

const queue = new PQueue({ concurrency: cpus().length })

export function runWorker<T>(filenameWithoutExtension: URL, workerData?: unknown): Promise<T> {
  return queue.add(async () => {
    const worker =
      process.env.NODE_ENV === 'test'
        ? new Worker(new URL(`${filenameWithoutExtension}.ts`), {
            workerData,
            execArgv: ['--loader', 'ts-node/esm/transpile-only'],
          })
        : new Worker(new URL(`${filenameWithoutExtension}.js`), { workerData })

    const result = await new Promise<T>((resolve, reject) => {
      worker.on('message', resolve)
      worker.on('error', reject)
      worker.on('exit', code => {
        if (code !== 0) {
          reject(new Error(`Worker stopped with exit code ${code}`))
        }
      })
    })

    return result
  })
}

// worker.ts
import { workerData, parentPort } from 'worker_threads'

export type WorkerData = number[]
export type WorkerResult = number

const numbers = workerData as WorkerData

const result: WorkerResult = numbers.reduce((prev, curr) => prev + curr, 0)

parentPort!.postMessage(result)

// index.ts
import type { WorkerData, WorkerResult } from './worker.js'
import { runWorker } from './runWorker.js'

const workerData: WorkerData = [1, 2]

const result = await runWorker<WorkerResult>(
  new URL('./worker', import.meta.url),
  workerData,
)

Source: andywer#434 (comment)

@trasherdk
Copy link
Owner Author

Very simple worker thread abstraction for ts-node ESM builds.

threads-esm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant