send event from worker to main outside of a function
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:
- Why would this only need to work in the special case of having 1 worker?
- Can you explain your use case?
Hi, josdejong,
- I make a customer script run in sandbox env, so I just need 1 worker.
- feature need,worker need send something to main directly, actullay, I also need workerEmit as async(wait main reply)
- 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?
- Yes I understand that you want to send "something" but what is that? Can you explain your use case in high level?
- 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
})
- 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.
- 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
onCreateWorkerto allow for that? Or is the current functionality ofonCreateWorkeralready sufficient for what you need?
sure, and maybe we can add promiseMap in worker side to imple async emit like main side. thanks.