最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

文件系统的读写:这是一个竞争条件?

运维笔记admin12浏览0评论

文件系统的读/写:这是一个竞争条件?

文件系统的读/写:这是一个竞争条件?

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

发布评论

评论列表(0)

  1. 暂无评论