[fix] readdir synchronously

This commit is contained in:
Wu Han 2021-01-12 12:17:37 +00:00
parent 098c886638
commit 04477643ec
1 changed files with 44 additions and 48 deletions

View File

@ -115,63 +115,59 @@ var update_submission = (socket, sessionID) => {
socket.emit('info', 'Got output') socket.emit('info', 'Got output')
// Read output dir // Read output dir
fs.readdir(output_dir, (err, files) => { files = fs.readdirSync(output_dir)
if (err) { // Read each result in txt file
console.log(err) files.forEach(file => {
return if (path.extname(file) === '.txt') {
} var i = 0
// Read each result in txt file // Result object
files.forEach(file => { var obj = {}
if (path.extname(file) === '.txt') { obj.name = file
var i = 0
// Result object try {
var obj = {} // read contents of the file
obj.name = file const data = fs.readFileSync(output_dir + file, 'UTF-8')
try { // split the contents by new line
// read contents of the file const lines = data.split(/\r?\n/)
const data = fs.readFileSync(output_dir + file, 'UTF-8')
// split the contents by new line lines.forEach((line) => {
const lines = data.split(/\r?\n/) i = i + 1
// The first line is the fitness
lines.forEach((line) => { if(i == 1) {
i = i + 1 var fitness = parseFloat(line)
// The first line is the fitness if(fitness < 0) {
if(i == 1) { obj.fitness = parseInt(line)
var fitness = parseFloat(line)
if(fitness < 0) {
obj.fitness = parseInt(line)
}
else {
obj.fitness = fitness
}
} }
// The second line is the path else {
else if (i == 2) { obj.fitness = fitness
// Only read when there is no error
if(obj.fitness > 0) {
solution_array = line.split(",")
for(var j = 0; j < solution_array.length; j++) {
solution_array[i] = parseInt(solution_array[i], 10)
}
obj.solution = solution_array
}
else {
obj.solution = []
}
} }
}) }
} catch (err) { // The second line is the path
console.error(err) else if (i == 2) {
} // Only read when there is no error
res.push(obj) if(obj.fitness > 0) {
solution_array = line.split(",")
for(var j = 0; j < solution_array.length; j++) {
solution_array[i] = parseInt(solution_array[i], 10)
}
obj.solution = solution_array
}
else {
obj.solution = []
}
}
})
} catch (err) {
console.error(err)
} }
}) console.log(obj)
res.push(obj)
}
}) })
if (res.length === 0) { if (res.length === 0) {
console.log(res)
socket.emit('error', 'Please check my_model.py') socket.emit('error', 'Please check my_model.py')
} }
socket.emit('result', JSON.stringify(res)) socket.emit('result', JSON.stringify(res))