Qt or PyQt - check when file is used by another process. Wait until finish copy

余生颓废 提交于 2019-12-08 13:57:28

问题


Good morning, What is the best strategy for check when a big file o big directory has finished to copy? I want wait until a file has finish fully to copy. Is there a code example in q

I'm working on mac os x.

thanks

Update

I use QFileSystemWatcher. the problem is that I receive file or directory change notification when o copy it is in progress. So user copy a big folder (inside many files), the operating system copy process start, it take 5 minuts, but in same times my application receive file changed notification. This is a problem because when i receive a change notification my application start for doing some operations on that files, but the copy is already in progress!!!!


回答1:


I think that the QFileSystemWatcher is the right start for you to get to the point of monitoring for changes, but as you have found, these changes are ANY changes. From this point, I think it should be easy enough for you to just check the modification time of the file.

Here is a simple example of a Watcher class that will let you specify a file to monitor and see if it has been modified after a given time. It can run a callback or emit a signal that anyone can watch:

import os.path
import time

from PyQt4 import QtCore

class Watcher(QtCore.QObject):

    fileNotModified = QtCore.pyqtSignal(str)

    MOD_TIME_DIFF = 5 #seconds

    def __init__(self, aFile, callback=None, checkEvery=5):
        super(Watcher, self).__init__()

        self.file = aFile
        self.callback = callback

        self._timer = QtCore.QTimer(self)
        self._timer.setInterval(checkEvery*1000)
        self._timer.timeout.connect(self._checkFile)

    def _checkFile(self):
        diff = time.time() - os.path.getmtime(self.file)
        if diff > self.MOD_TIME_DIFF:
            self._timer.stop()

            self.fileNotModified.emit(self.file)

            if self.callback:
                self.callback()

    def start(self):
        self._timer.start()

    def stop(self):
        self._timer.stop()

An example of using it:

def callbackNotify():
    print "Callback!"

def signalNotify(f):
    print "Signal: %s was modified!" % f

# You could directly give it a callback
watcher = Watcher("/path/to/file.file", callback=callbackNotify)
# Or could use a signal
watcher.fileNotModified.connect(signalNotify)
# tell the watcher timer to start checking
watcher.start()

## after the file hasnt been modified in 5 seconds  ##
# Signal: /path/to/file.file was modified!
# Callback!



回答2:


There is only one reliable way to do this: Change the copy process to write to temporary files and then rename them after the copy is finished.

That way, you can ignore new files which end with .tmp and rename is an atomic operation.

If you can't change the copy process, all you can do is add a timer to wait for, say, half an hour to make sure the copy is really finished.

A more fine grained (and more risky) approach is to add a loop that check the file size and stops when the file size doesn't change for a certain time but that's also hard to get right.

Worse, this doesn't prevent you from reading partial files (when the copy process was terminated in the middle).




回答3:


Try using QtConcurrent framework. In particular, check out QFuture and QFutureWatcher. You can execute asynchronous copy operations inside a QFuture object and monitor its progress through signals and slots with a watcher.

bool copyFunction() {
  // copy operations here, return true on success
}

MyHandlerClass myObject;
QFutureWatcher<bool> watcher;
connect(&watcher, SIGNAL(finished()), &myObject, SLOT(handleFinished()));
QFuture<bool> future = QtConcurrent::run(copyFunction);



回答4:


Since you have no control on the external application, my suggestion is that you lock the files while you work on them. In this way other programs will not be able to access them while locked.

Alternatively, if you have access to the other program's source, you should implement some form of inter process communication,via sockets, messages or whatever method you prefer.



来源:https://stackoverflow.com/questions/9681870/qt-or-pyqt-check-when-file-is-used-by-another-process-wait-until-finish-copy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!