Abr*_*m P 10 javascript asynchronous promise
假设我有一个目录foo,有一些子目录.这些子目录中的每一个都有0到5个可变长度的文件,我想处理它们.我的初始代码如下:
pool.query(`
SET SEARCH_PATH TO public,os_local;
`).then(() => fs.readdirSync(srcpath)
.filter(file => fs.lstatSync(path.join(srcpath, file)).isDirectory())
.map(dir => {
fs.access(`${srcpath + dir}/${dir}_Building.shp`, fs.constants.R_OK, (err) => {
if (!err) {
openShapeFile(`${srcpath + dir}/${dir}_Building.shp`).then((source) => source.read()
.then(function dbWrite (result) {
if (result.done) {
console.log(`done ${dir}`)
} else {
const query = `INSERT INTO os_local.buildings(geometry,
id,
featcode,
version)
VALUES(os_local.ST_GeomFromGeoJSON($1),
$2,
$3,
$4) ON CONFLICT (id) DO UPDATE SET
featcode=$3,
geometry=os_local.ST_GeomFromGeoJSON($1),
version=$4;`
return pool.connect().then(client => {
client.query(query, [geoJson.split('"[[').join('[[').split(']]"').join(']]'),
result.value.properties.ID,
result.value.properties.FEATCODE,
version
]).then((result) => {
return source.read().then(dbWrite)
}).catch((err) => {
console.log(err,
query,
geoJson.split('"[[').join('[[').split(']]"').join(']]'),
result.value.properties.ID,
result.value.properties.FEATCODE,
version
)
return source.read().then(dbWrite)
})
client.release()
})
}
})).catch(err => console.log('No Buildings', err))
}
})
fs.access(`${srcpath + dir}/${dir}__ImportantBuilding.shp`, fs.constants.R_OK, (err) => {
//read file one line at a time
//spin up connection in pg.pool, insert data
})
fs.access(`${srcpath + dir}/${dir}_Road.shp`, fs.constants.R_OK, (err) => {
//read file one line at a time
//spin up connection in pg.pool, insert data
})
fs.access(`${srcpath + dir}/${dir}_Glasshouse.shp`, fs.constants.R_OK, (err) => {
//read file one line at a time
//spin up connection in pg.pool, insert data
})
fs.access(`${srcpath + dir}/${dir}_RailwayStation.shp`, fs.constants.R_OK, (err) => {
//read file one line at a time
//spin up connection in pg.pool, insert data
})
})
Run Code Online (Sandbox Code Playgroud)
这主要是有效的,但它最终必须等待在每个子目录中完全处理最长的文件,导致实践中始终只有1个连接到数据库.
有没有一种方法可以重新构建这个以更好地利用我的计算资源,同时限制活动postgres连接的数量并强制代码等到连接可用?(我在pg poolConfig中将它们设置为20,用于node-postgres)
| 归档时间: |
|
| 查看次数: |
320 次 |
| 最近记录: |