dongwu3747 2018-12-11 16:58
浏览 18
已采纳

如何打印目录中文件的内容,但忽略以写模式打开的文件?

I have a goroutine which periodically checks for new files in a directory and then prints the contents of the files. However there is another goroutine which creates a file, writes contents into it and then saves the file.

How do I ignore the files which are open in WRITE mode in a directory?

Sample Code:

for {
 fileList, err := ioutil.ReadDir("/uploadFiles")
  if err != nil {
     log.Fatal(err)
     continue
  }

 for _, f := range fileList {
      log.Println("File : ", f.Name())
      go printContents(f.Name())
 }

 time.Sleep(time.Second * 5)
}

In the printContents goroutine I want to ignore the files which are open in WRITE mode.

  • 写回答

1条回答 默认 最新

  • dqe55175 2018-12-11 17:33
    关注

    That is not how it's done.

    Off the top of my head I can think of these options:

    • If both goroutines are working in the same program, there is little problem: make the "producer" goroutine register the names of the files it has completed modifying into some registry, and make the "consumer" goroutine read (and delete) from that registry.

      In the simplest case that could be a buffered channel.

      If the producer works much faster than the consumer, and you don't want to block the former for some reason then a slice protected by a mutex would fit the bill.

    • If the goroutines work in different processes on the same machine but you control both programs, make the producer process communicate the same data to the consumer process via any suitable sort of IPC.

      What method to do IPC is better depends on how the processes start up, interact etc.

      There is a wide variety of options.

    • If you control both processes but do not want to mess with IPC between them (there are reasons, too), then make the producer follow best practices on how to write a file (more on this in a moment), and make the consumer use any filesystem-monitoring facility to report which files get created ("appear") once produced by the producer. You may start with github.com/fsnotify/fsnotify.

      To properly write a file, the producer have to write its data to a temporary file—that is, a file located in the same directory but having a filename which is well understood to indicate that the file is not done with yet—for instance, ".foobar.data.part" or "foobar.data.276gd14054.tmp" is OK for writing "foobar.data". (Other approaches exist but this one is good enough to start with.)

      Once the file is ready, the producer have to rename the file from its temporary name to its "proper", final name. This operation is atomic on all sensible OSes/filesystems, and makes file atomically "spring into existense" from the PoV of the consumer. For instance, inotify on Linux generates an event of type "moved to" for such an appearance.

      If you don't feel like doing the proper thing yourself, github.com/dchest/safefile is a good cross-platform start.

      As you can see, with this approach you know the file is done just from the fact it was reported to having appeared.

    • If you do not control the producer, you may need to resort to guessing.

      The simpest is to, again, monitor the filesystem for events—but this time for "file updated" events, not "file created" events. For each file reported as updated you had to remember the timestamp of that event, and when certain amount of time passes, you may declare that the file is done by the producer.

      IMO this approach is the worst of all, but if you have no better options it's at least something.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳