josdejong/workerpool

send event from worker to main outside of a function

frankie-zeng opened this issue · 8 comments

when the worker counte is one (min:1,max:1), woker should can emit event outside function, pool can on('event') global.

If I understand you correctly you would like to be able to send an event from a worker to the main thread? There already is a way to send an event (like progress) from the worker to the main thread using the exec option { on: (payload: any) => void }. Is that what you mean?

See https://github.com/josdejong/workerpool#events

like this.


const pool=...
pool.on('xxx")

worker.workerEmit in anywhere, not only in function

Ah, ok so you would like to call worker.workerEmit() outside of a function. We can think that through. Two questions:

  1. Why would this only need to work in the special case of having 1 worker?
  2. Can you explain your use case?

Hi, josdejong,

  1. I make a customer script run in sandbox env, so I just need 1 worker.
  2. feature need,worker need send something to main directly, actullay, I also need workerEmit as async(wait main reply)
  1. I'm not a fan of introducing some logic that only works in the specific case of having 1 worker. Can't it just work too when multiple workers?
  2. Yes I understand that you want to send "something" but what is that? Can you explain your use case in high level?
  1. my worker will access single hardware(only one,node addon), so I just need 1 worker.
/* always import uds first*/
import { workerEmit } from 'workerpool'
import {uds} from './lib/uds'
import * as fs from 'fs'

//it can't work in current version
console.log('started')

//it can't work in current version
workerEmit({register:'JobFunction0'})


uds.on('Seq:DiagnosticSessionControl16:preSend',async function (val){
    //it doesn't support async function
    await workerEmit({event:'Seq:DiagnosticSessionControl16:recv',data:Buffer.from('')})
    await workerEmit({event:'Seq:DiagnosticSessionControl16:recv',data:Buffer.from('')})

    //it need wait main done the event then return
    return 
})  

  1. ok then I take it that it is fine too if this works with multiple workers though you do not need that for your current use case.
  2. thanks. So it looks like you want to send some information from the worker to the main process at creation of the worker. Maybe we can extend onCreateWorker to allow for that? Or is the current functionality of onCreateWorker already sufficient for what you need?

sure, and maybe we can add promiseMap in worker side to imple async emit like main side. thanks.