gpt4 book ai didi

javascript - 文件系统读/写 : Is this a race condition?

转载 作者:行者123 更新时间:2023-12-03 00:16:36 26 4
gpt4 key购买 nike

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.com/bcherny/029473f21833a73126d2e1dce53f2a6a

关于javascript - 文件系统读/写 : Is this a race condition?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54509334/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com