文件系统的读/写:这是一个竞争条件?
import {appendFile, readFile} from 'fs'
// Read data from an Apache server's access log
readFile(
'/var/log/apache2/access_log',
{encoding: 'utf8'},
(error, data) => {
if (error) {
console.error('error reading!', error)
return
}
console.info('success reading!', data)
}
)
// Concurrently, write data to the same access log
appendFile(
'/var/log/apache2/access_log',
'New access log entry',
error => {
if (error) {
console.error('error writing!', error)
}
})
它是保证读将完成前appendFile
写入到文件系统,或者是有可能的是,readFile
完成之前,我的数据可能会追加,使readFile
返回我的新追加的数据?
有一个快速测试尝试了这一点,它确实是不稳定的:
https://gist.github/bcherny/029473f21833a73126d2e1dce53f2a6a