import { SHARE_ENV , Worker } from 'worker_threads' ; import Path from 'path' ; import type { TensorFlowHubModel } from '../imagenet' ; export interface ClassifierWorkerRequest { definition : TensorFlowHubModel ; absolutePath : string ; } export interface ClassifierWorkerError { err : string ; } export interface ClassifierWorkerSuccess { probabilities : string [ ] ; } export type ClassifierWorkerResponse = ClassifierWorkerError | ClassifierWorkerSuccess ; export class ClassifierWorkerWrapper { private worker = new Worker ( Path . resolve ( __dirname , 'classifier-worker.js' ) , { env : SHARE_ENV , stdout : true , stderr : true , } ) ; public async quit ( ) { await this . worker . terminate ( ) ; } public classify ( request ) { return new Promise < ClassifierWorkerSuccess > ( ( resolve , reject ) => { this . worker . once ( 'message' , ( response ) => { if ( ( response as ClassifierWorkerError ) . err ) { reject ( new Error ( ( response as ClassifierWorkerError ) . err ) ) ; } else { resolve ( response as ClassifierWorkerSuccess ) ; } } ) ; this . worker . postMessage ( request ) ; } ) ; } }