F
F
Flakelf2018-10-20 17:00:53
Python
Flakelf, 2018-10-20 17:00:53

How to run a function on multiple threads in Node JS?

How to run a function in multiple threads?
Let's say I have a function that makes ten GET requests to some URL in a loop:

function makeReq(param1, param2) {
    for(i = 0, i < 10, i++) {
        // Do something
    }
}

I want to run it, let's say, in a hundred threads with different arguments, in order to simulate a thousand requests, the threads do not communicate with each other, consider it should be like a separate process that launches the parent process. I know that it is possible to run child_process through some libraries, but is this the only solution? And I know NODE.JS is single threaded.
By the way, how much will I lose in performance, or is it better to write a simple console in Python with its threading?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
napa3um, 2018-10-20
@napa3um

https://nodejs.org/api/cluster.html
On a node, it is very difficult to lose performance to python (in the V8 code, everything is optimized by bloody enterprise competition, while python is grown in a more relaxed atmosphere of laboratory research, when it is not very important, the script will be executed in one second or ten). In your case, in general, everything rests on IO, you can write at least in BASIC, the interpreter itself will not slow down, it will constantly wait for IO.
And just in case, I'll ask - why do you need to send requests in several threads? After all, they are still serialized in TCP and will go in order. Very similar to a naive attempt to implement didos :).

I
Ivan Shumov, 2018-10-20
@inoise

Queues (rabbitmq, Kafka) + many workers

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question