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

Use worker threads to calculate every indicator individually #310

Open
Vasile-Peste opened this issue Jun 16, 2023 · 6 comments
Open

Use worker threads to calculate every indicator individually #310

Vasile-Peste opened this issue Jun 16, 2023 · 6 comments
Labels
enhancement New feature or request

Comments

@Vasile-Peste
Copy link

It would be cool to use https://nodejs.org/api/worker_threads.html
To calculate every indicator in a separated thread, maybe by passing a useWorker: true option to every indicator

Regards,
Vasile

@cinar
Copy link
Owner

cinar commented Jun 16, 2023

This is a great idea. I wonder how to do this in a way that it can run on both the browser and the node, meanwhile, without hopefully touching each indicator as they don't currently take any optional parameters. Is there a library that you are using that is doing this well? I can take a look at how they did the api for this to perhaps align on that common approach?

@Vasile-Peste
Copy link
Author

Workers are available also on browsers! And the API is quite the same as in Node.js!

I have explored all GitHub public repositories up to date and didn't find anyone using such method. So it would be a great plus here!

I can help if needed, but can't do a whole PR because of lack of time and higher priorities

Regards,
Vasile

@cinar
Copy link
Owner

cinar commented Jun 16, 2023

If you can perhaps make an example of what you are envisioning, I can do my best to apply to all indicators here. If there is a way we can do this without changing the method signature of the indicators but wrapping them somehow, that would be awesome actually. I'm worried about making a breaking change. I'm sure we can find a way around it.

@Vasile-Peste
Copy link
Author

Vasile-Peste commented Jun 19, 2023

Take this issue just as a cool idea, if you don't have time or feel people don't want it, just wait for the right time to implement it, let's go:

This is the example provided in the readme

import { awesomeOscillator, } from "indicatorts";

const highs = [ 10, 20, 30, 40, ];
const lows = [ 1, 2, 3, 4, ];

const ao = awesomeOscillator(highs, lows);

To avoid breaking the API (and also multithreading should be optional) we could do as follows

import { useWorker, } from "indicatorts";

const ao = await useWorker("awesomeOscillator", ...params);

Note the async here, since we are dealing with an external thread this operations is going to be asynchronous and not block our main thread.

The implementation of useWorker would be the following (NOT TESTED yet)
which should be pretty cool because it doesn't need external files

const { Worker } = require("node:worker_threads");

export function useWorker (name, ...params) {
    const workerProcess = () => {
        const { parentPort } = require("node:worker_threads");

        self.onmessage = function (message) {
            const directives = JSON.parse(message.data);
            const indicator = require("indicatorts")[directives.name];
            const result = indicator(...directives.params);

            parentPort.postMessage(JSON.stringify(result));
        };
    };

    const worker = new Worker(workerProcess.toString().substr(6), { eval: true, });

    worker.postMessage(JSON.stringify({ name, params, }));

    return new Promise((resolve) => {
        worker.on("message", (message) => {
            worker.terminate();
            resolve(JSON.parse(message));
        });
    });
}

@theolundqvist
Copy link

Would it be possible to do the following instead

useWorker(awesomeOscillator, ...params)

otherwise we loose all autocompletion

@cinar
Copy link
Owner

cinar commented Oct 21, 2023

There seems to be enough interest now on having something like this. Let me see if I can add it.

@cinar cinar added the enhancement New feature or request label Oct 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants