简体   繁体   中英

Node.js multithread program

I wrote a node.js script to fetch some prices from exchanges. It looks like this:

async function main() {
  async function func() {
    var start_time = performance.now();
    for (let route of routes) {
      var result_amount = await calc(route, amount_wei);
          if (result_amount[5] > amount_start * 1) {
              console.log("Good Trade");
    }
  while (true) {
     await func();
  }
}

and one route (route of routes) looks like this:

[
    "quick / sushi - 1x1",
    token_Address_usdc,
    token_Address_dai,
    token_Address_usdc,
    "-",
    "-",
    "USDC - DAI - USDC",
  ]

So first I am fetching the output if I swap usdc to dai on quickswap. Then from dai to usdc on sushiswap. I save the output in an array ( result_amount ) and give it back to the main program (Now result is compared to the start amount).

I do have like 10 trading routes and the program needs about 20 seconds, so 2 seconds per route. The routes are absolutely independent from each other, so it should be possible to fetch all routes at the same time right? I have read something about multi threads with workers , but I have to say, I didn't get it. Can someone help me with this problem?

Thank you

Stop using await and use Promise.all

This will allow you to wait for all of your data to come in in parallel, rather than serially

function func() {
  var promises = [];
  for (let route of routes) {
    promises.push (calc(route, amount_wei));
  }
  Promise.all(promises).then(function(completedItems) {
    completedItems.forEach(function(val) {
      var result_amount = val;
      if (result_amount[5] > amount_start * 1) {
        console.log("Good Trade");
      }
    }
  });
}

It's important to understand that node.js is generally single threaded and not multithreaded. Even though asynchronous operations sometime give the impression of parallelism, it is not a requirement. Meaning, just because an operation is asynchronous, does not mean it has to run in its separate thread.

The routes are absolutely independent from each other, so it should be possible to fetch all routes at the same time right?

It depends, the easiest case would be complete independence, where the result of said request isn't used in conjunction with the results of other fetches.

You really didn't provide too much detail, so it's not really possible to answer your question.

What I can recommend, though, is: always try to avoid multithreading if possible.

ControlAltDel 's answer is a good starting point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM