我如何等待文件的写入? [英] How can I wait for the write of a file?

查看:106
本文介绍了我如何等待文件的写入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我执行该程序时,它运行良好,但验证返回false.如果我重新执行它,则验证有效.

When I execute this program, it works well but the verification returns false. If I re-execute it, the verification works.

fullpath是备份的目录,而refpath是原始文件的路径:

fullpath is the directory of the backup, and refpath is the path to the original files:

if (fullpath.include?(refpath) && refpath.empty? == false && fullpath.empty? == false)
  diffpath= "#{fullpath} #{refpath}"
  puts diffpath
  sortie = IO.popen("diff -Bb #{diffpath}").readlines #(fullpath backup_dir)
  #puts fullpath
  if sortie.empty?

    puts "Les fichiers -#{f} sont identiques."

  else
    puts "Modification : [#{refpath}] \n [#{fullpath}] "
  end
end 

主程序是:

require "modif.rb"
require "testdate.rb"
require "restore_data.rb"

#Pour la sauvegarde des fichiers
puts "__________SAUVEGARDE__________"

#Pour la restauration des fichiers :
puts "__________RESTAURATION__________"

#Vérification de l'intégrité des fichiers restaurés.
puts "__________VERIFICATION__________"
sleep(5.0)
v = Verif.new
v.do_verif(outdir)

当我打开还原文件的目录时,文件没有完全写入.

When I open the directory where the files are restored, the files are not completely written.

在调用验证之前,我先调用保存,备份和验证.

Before calling the verification, I call save, backup and verification.

sleep不起作用.该过程已完全暂停,不会写入丢失的文件.

The sleep doesn't work. The process is completely paused and won't write the missing files.

推荐答案

您的原始文件有多少GB?我怀疑sleep 5.0是否真的没有意义,并且根本原因是其他原因.还是将慢速USB闪存用作备份目录?

How many gigabytes does your original file have in size? I suspect if sleep 5.0 isn't really meaningful and the root cause is something else. Or do you use a slow USB flash memory as the backup directory?

如果确定需要等待写入过程完成,则可以对备份文件的mtime进行轮询:

If you are sure that you need to wait for the writing process to complete, perhaps you can do polling on mtime of the backup file:

finished = false
30.times { # deadline of 30*10 == 300 seconds
  if 5 < (File.mtime(fullpath) - Time.now).abs
    # the backup process had done its job
    finished = true
    break
  end
  sleep 10
}

if finished
  v = Verif.new
...

当备份过程正在写入输出文件时,File.mtime(fullpath)应该在距Time.now 2秒以内.注意具有2秒时间分辨率的FAT文件系统.我还使用了abs,因为某些备份程序会根据需要修改mtime的值.

When the backup process is in the middle of writing into the output file, File.mtime(fullpath) should be within 2 seconds from Time.now Be careful of the FAT filesystem with 2 seconds time resolution. I also used abs because some backup programs modify mtime value as they want.

这篇关于我如何等待文件的写入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆