Skip to content

Feature request: accept function or block in worker_threads as starting point of execution #21610

@AyushG3112

Description

@AyushG3112

This is a crude proposal of a direction I think we can take worker_threads forward in, and I thought I should post it to get community feedback.

I do understand that we are trying to align worker_threads with WebWorkers, but considering the fact that server requirements are different from client, and to take advantage of the fact that workers have their own event loop, I think we should support doing the following:

let aVeryLargeArray = [1, 2, 3, 4, 5 .... 100000000];
const workerThreads = require('worker_threads');
const messageChannel = workerThreads.start((channel, input) => {
    const resultArray = input.map((item) => item * 2);
    channel.port1.postMessage({ resultArray });
}. aVeryLargeArray); // just like setTimeout with the channel reference as an input to the function
messageChannel.port1.on("message", ({resultArray}) => {
    console.log("The result array obtained is ": resultArray);
    channel.port1.close();
});
messageChannel.port1.start();

The main benefit of this which I see that it would make it easier to manipulate very large data sets on the server, synchronously if needed, without starving the event loop of the main thread(kind of using the workers like how fs uses the UV Thread Pool)

/cc @addaleax @nodejs/workers

EDIT:

Another option we could use would be blocks

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues that request new features to be added to Node.js.workerIssues and PRs related to Worker support.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions