donglu1973 2017-10-16 09:57 采纳率: 100%
浏览 28

等待文件忙另一个进程

I try to create some deamon which look the folde and if we insert new file open this file and do some magic, i use this (https://github.com/howeyc/fsnotify) FS notificator , but if i open file i have error "The process cannot access the file because it is being used by another process." i find a dirty solution whith time sleep 100 ms , but i think this is not clear way

func main(){
watcher, err := fsnotify.NewWatcher()
if err != nil {
    log.Fatal(err)
}

done := make(chan bool)

// Process events
go func() {
    for {
        select {
        case ev := <-watcher.Event:
            if ev.IsCreate() == true {
                time.Sleep(100 * time.Millisecond)
                 file,err := os.OpenFile(path+ev.Name,os.O_RDWR|os.O_EXCL,0755)
                 if err !=nil{
                   panic(err)
                 }
                 //some code
                 file.Close()
            }
        case err := <-watcher.Error:
            log.Println("error:", err)
        }
    }
}()

err = watcher.Watch("test")
if err != nil {
    log.Fatal(err)
}

<-done

watcher.Close()
}

UPD: Why do I have to do this ? I have a task to make the database Webm files , in order to determine if I have already such a file in the database I should get the hash of a file , but in the specification of this format (https://matroska.org/technical/specs/index.html) is the global bit Void block which stores some information , due to the fact that this unit can store different information from the same file will have different hash values , for this I use my function to find this bit block and reset it.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?
    • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
    • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计