nextron icon indicating copy to clipboard operation
nextron copied to clipboard

Child process not working in packaged app

Open kentezrabetita opened this issue 1 year ago • 7 comments

Hello,

I am experiencing an issue related to the execution of a child process in the packaged version of my Nextron application. The child process works as expected in the development environment, but fails to execute when the application is packaged. I am currently testing on an M1 Mac.

The file structure in the main directory is as follows:

main/
├─ helpers/
├─ background.ts
├─ ipc.ts
├─ worker.ts

In my ipc.ts file, I fork a child process from worker.ts:

const child = fork('./main/worker.ts'); // i think the problem also lies here

child.on('message', (message) => {
  console.log('Received message from child:', message);
});

child.on('exit', (code) => {
  console.log(`Child process exited with code ${code}`);
});

export const setupIpcHandlers = () => { // this function is called in the `background.ts`
  ipcMain.on('store-image', async (event, image) => {
    const user: any = state.store.get('user_data');
    const { id } = user;

    if (child.connected) {
      child.send({ image: image, user_id: id });
    }
  });
}

In the worker.ts file, I handle the 'message' event from the main process:

process.on('message', async (message) => {
  // handles a basic HTTP request to upload image
});

In development, the main process successfully sends the message and the child process performs as expected. However, in the packaged app, the child process doesn't seem to be executing.

Despite adding 'exit' and 'error' event handlers to the child process, no error messages or exit codes have been generated.

Upon examining the built version of the app in the ./app directory, I noticed the absence of a worker.js file or other files that i've added. However, references to the worker code exist in background.js. This leads me to suspect that the issue might be related to the way I'm referencing the worker file in const child = fork('./main/worker.ts');.

Any guidance on this issue would be greatly appreciated, particularly on getting the child process to work correctly in the packaged app.

Thank you!

kentezrabetita avatar Dec 05 '23 01:12 kentezrabetita

Hi @kentezrabetita, I managed to include a child process running Python or Rust code via a call from IPC.

I'm about to push it tomorrow or Thursday in another branch, then do a PR to the examples folder of Nextron.

You will be able to use the analogy in .ts, .js easily, but for Python, you need to have Python or Rust installed on the target machine to ruin your app. As I said, if you only use js/ts, no need, it is already packed in Nextron, you just. <I will let you need when it is done>

bm777 avatar Dec 05 '23 13:12 bm777

Hey @bm777, that's awesome news! I appreciate your work on this and thanks for the good heads up. Looking forward to your PR!

kentezrabetita avatar Dec 06 '23 00:12 kentezrabetita

@kentezrabetita Here is the example running Python code via IPC on top of the basic lang javascript example.

  • it handles dev mode and pro mode in the background.js
  • Waiting for the merge or review of the PR.

https://github.com/saltyshiomix/nextron/assets/29865600/be3305cb-85a2-4890-a396-5aa0847dedf9

bm777 avatar Dec 07 '23 00:12 bm777

you not passed channel when called child.send()

JesTery58 avatar Jan 25 '24 08:01 JesTery58

@JesTery58 can you be more precise please?

bm777 avatar Jan 25 '24 08:01 bm777

do child.send() function want a channel ?

JesTery58 avatar Jan 25 '24 09:01 JesTery58

@JesTery58 basically when using IPC, I always pass the channel as str, in this case, the channel was : here

bm777 avatar Jan 25 '24 10:01 bm777