A
A
Alex2017-07-31 13:22:04
Node.js
Alex, 2017-07-31 13:22:04

How to move the execution of a function to a separate process and return the result?

I am writing an application in Electron. In it, a function is called doWithArray ()that converts a multidimensional array. The problem is that often this array is huge. And it takes a long time to process. And for the duration of the processing, the application interface does not respond.
Is it possible to execute this function in a separate process so that the interface is not "frozen" and then return the result to the parent?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
RidgeA, 2017-07-31
@RidgeA

Something like this.

let promise = new Promise((resolve, reject) => {
    doWithArray(...)
    resolve(...)
}).then(...).catch(...)

This is the easiest option. It's not in a separate process - it's asynchronous. Those. calculations will be carried out in the same thread, but will not interfere with the main process - the interface will not be frozen.
To really put it into a separate process, you need to use https://nodejs.org/api/child_process.html

C
Coder321, 2017-07-31
@Coder321

const fork = require('child_process');
const childModule = fork('./your_module');
childModule.on('message', callback)

Create a module from your function, fork it, put handlers.
fork

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question