I need to run a function int f(int i)
with 10_000 parameters and it takes around 1sec to execute due to I/O time.
In a language like Python, I can use threads (or async/await
, I know, but I'll talk about it later) to parallelize this task.
If I want to always have 10 running threads, and to split the task between them, I can use ThreadingPool :
def f(p):
x = [...]
return x
p = ThreadPool()
xs = p.map(f, range(10_000))
But how does it work ? If I want to implement a similar thing with, let's say NodeJS and f = http("www.google.com", callback)
, where should I begin ? What's the algorithms for this kind of problem ?
Again, I'd like to get 10 requests at the same time, and when one is finished the next one should start.
What I've been thinking so far (ugly because the callback is starting a new call to the f() function):
queue = ["www.google.com", "www.facebook.com"]
var f = function(url) {
http.get(url, (e) => {
const newUrl = queue.pop();
f(newUrl);
});
};
for (var i = 0; i < 10; i++) {
f(queue.pop());
}