josdejong / workerpool

Offload tasks to a pool of workers on node.js and in the browser
Apache License 2.0
2.04k stars 148 forks source link

send event from worker to main outside of a function #437

Open frankie-zeng opened 5 months ago

frankie-zeng commented 5 months ago

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

josdejong commented 4 months ago

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

frankie-zeng commented 4 months ago

like this.


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

worker.workerEmit in anywhere, not only in function
josdejong commented 4 months ago

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?
frankie-zeng commented 4 months ago

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)
josdejong commented 4 months ago
  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?
frankie-zeng commented 4 months ago
  1. my worker will access single hardware(only one,node addon), so I just need 1 worker.
  2. 
    /* 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 

})

josdejong commented 4 months ago
  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?
frankie-zeng commented 4 months ago

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