0

我有这段代码,我尝试在使用它们通过库执行 HTTP 请求之前缓存必要的内存。我之前缓存它们以使代码在实际执行请求时更快,这对于尽可能快地进行至关重要。

该代码正在运行,但现在它以“同步”方式处理请求。

我相信问题代码是以下行:

for (i = 0; i < exchanges.length; i++)

我不确定运行上述 for 循环异步的最佳/最快方法是什么?

'use strict';
const ccxt = require('ccxt');
const fs = require('fs');
const path = require('path');

//Cache some memories first
var exchangesArray = [];
var exchangesArray2 = [];
(async () => {
  const allexchanges = ccxt.exchanges.filter((id) => !['coinmarketcap', 'theocean'].includes(id))
        .map(async (id) => {
            const Exchange = ccxt[id];
            const exchange = new Exchange({ enableRateLimit: true });
            if (exchange.has['fetchTickers']) {

                exchangesArray.push(exchange);
                exchangesArray2.push(id);
            }
        });

    await Promise.all(allexchanges);
})();



//The cached memories
const exchanges = exchangesArray; //Holds exchanges object
const exchangesnames = exchangesArray2; //Holds exchanges name
var i;

//Use cached memories to do the "fetchTickers()" as fast as possible now
(async () => {
    console.log(`start`);
    const start = Date.now();


    //What is the fastest way to make this for loop async/await to run in parallel?
    for (i = 0; i < exchanges.length; i++) {

        // pop one exchange from the array
        const exchange = exchanges[i]
        const exchangename = exchangesnames[i]

        try {
            const tickers = await exchange.fetchTickers();
            const dumpFile = path.join(__dirname, 'tickers', `${exchangename}Tickers.json`);
            await fs.promises.writeFile(dumpFile, JSON.stringify(tickers));
        } catch (e) {
            console.error(e);
        }
    }


    // wait for all of them to execute or fail
    await Promise.all(exchanges)


    const end = Date.now();
    console.log(`Done in ${(end - start) / 1000} seconds`);
})();

4

2 回答 2

0

看一下for await...of语法。

于 2019-02-22T21:56:11.773 回答
0

试试这个。基本上,您为每个操作创建一组承诺以保存您的代码。然后使用 Promise.all 等待所有的代码进程解决。

//What is the fastest way to make this for loop async/await to run in parallel?
    var tickersPromises = []
    for (i = 0; i < exchanges.length; i++) {

        // pop one exchange from the array
        const exchange = exchanges[i]
        const exchangename = exchangesnames[i]

        try {
            let tickerProcessing = new Promise(async (resolve) => {
                // probably do a try catch in here
                const tickers = await exchange.fetchTickers();
                const dumpFile = path.join(__dirname, 'tickers', `${exchangename}Tickers.json`);
                await fs.promises.writeFile(dumpFile, JSON.stringify(tickers));
                resolve()
            })
            tickersPromises.push(tickerProcessing)

        } catch (e) {
            console.error(e);
        }
    }


    // wait for all of them to execute or fail
    await Promise.all(tickersPromises)

从高层次来看,如果你真的想让这段代码更快,请停止将响应写入文件并使用对象或字典库将它们存储在内存中。这就是说网络延迟将进入你最慢的部分。

我猜这些是加密货币交易所。请记住,在使用 REST api 时,代码通常会从加密交换中延迟。您最好使用许多交易所提供的 websocket 接口,以便在可用时尽快向您推送最新的股票信息,以最大程度地减少延迟。

于 2019-02-22T22:19:31.053 回答