最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

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

IT培训 admin 4浏览 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

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

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. 暂无评论