简体   繁体   English

Node.js 多线程程序

[英]Node.js multithread program

I wrote a node.js script to fetch some prices from exchanges.我写了一个 node.js 脚本来从交易所获取一些价格。 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.因此,如果我在 quickswap 上将 usdc 换成 dai,首先我会获取 output。 Then from dai to usdc on sushiswap.然后在 sushiswap 上从 dai 到 usdc。 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).我将 output 保存在一个数组 ( result_amount ) 中并将其返回给主程序(现在将结果与起始金额进行比较)。

I do have like 10 trading routes and the program needs about 20 seconds, so 2 seconds per route.我确实有 10 条交易路线,程序需要大约 20 秒,所以每条路线需要 2 秒。 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.我已经阅读了一些关于 multi threads with workers的内容,但我不得不说,我不明白。 Can someone help me with this problem?有人可以帮我解决这个问题吗?

Thank you谢谢

Stop using await and use Promise.all停止使用 await 并使用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.重要的是要了解 node.js 通常是单线程而不是多线程。 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. ControlAltDel的回答是一个很好的起点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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