交换同步循环到异步方法?
Exchange sync for loop to async approach?
我有这段代码,在使用它们对库进行 HTTP 请求之前,我尝试缓存必要的内存。我之前缓存它们是为了让代码在实际执行请求时更快,这对尽可能快地执行请求至关重要。
代码正在运行,但现在它以 "synchronous" 方式处理请求。
我相信问题代码是下面一行:
for (i = 0; i < exchanges.length; i++)
我不确定 best/fastest 上面的 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`);
})();
看看 for await...of
语法。
试试这个。基本上,您为每个操作创建一个承诺数组来保存您的代码。然后使用 Promise.all 等待所有 ticker 进程解决。
//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 接口,以便在可用时尽快向您推送最新的行情信息,以最大程度地减少延迟。
我有这段代码,在使用它们对库进行 HTTP 请求之前,我尝试缓存必要的内存。我之前缓存它们是为了让代码在实际执行请求时更快,这对尽可能快地执行请求至关重要。
代码正在运行,但现在它以 "synchronous" 方式处理请求。
我相信问题代码是下面一行:
for (i = 0; i < exchanges.length; i++)
我不确定 best/fastest 上面的 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`);
})();
看看 for await...of
语法。
试试这个。基本上,您为每个操作创建一个承诺数组来保存您的代码。然后使用 Promise.all 等待所有 ticker 进程解决。
//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 接口,以便在可用时尽快向您推送最新的行情信息,以最大程度地减少延迟。