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

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

Open
frankie-zeng opened this issue Mar 30, 2024 · 8 comments
Open

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

frankie-zeng opened this issue Mar 30, 2024 · 8 comments

Comments

@frankie-zeng
Copy link

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

@josdejong
Copy link
Owner

josdejong commented Apr 3, 2024

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
Copy link
Author

like this.


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

worker.workerEmit in anywhere, not only in function

@josdejong
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

  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
Copy link
Author

  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 
})  

@josdejong josdejong changed the title emit/event global when single worker send event from worker to main outside of a function Apr 10, 2024
@josdejong
Copy link
Owner

  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
Copy link
Author

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

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

No branches or pull requests

2 participants