No, this cannot be done because shutil.copy does not have the means to ensure progress.
But you can write your own copy function (or even develop code from shutil - do not indicate that this is one of the modules that includes a link to the source at the top, which means that it should be just as useful for sample code as for use as-is). Your function can, for example, perform the progress callback function as an additional argument and call it after each buffer (or every N buffers, or every N bytes, or every N seconds). Something like:
def copy(src, dst, progress):
Now this callback will still be called in the background thread, and not in the main thread. With most GUIs, this means that it cannot directly touch any GUI widgets. But most GUIs have the ability to post to the main thread's event loop from the background thread, so just make a callback. With Qt, you do this with signals and slots, just like in the main thread; there are many great lessons if you donβt know how to do this.
Alternatively, you can do it the way you suggested: transfer the main thread to the background thread (for example, by publishing to queue.Queue or run Event or Condition ) and have the copy function check this signal every time through the loop and respond. But it seems more complex and less responsive.
One more thing: Qt has its own thread library, and you can use it instead of the native Python, because you can attach the slot directly to the QThread object and make your callback. I'm not sure, but Qt may even have its own copy file copy methods somewhere; they try to complete everything that can be between different platforms and is vaguely connected with graphical interfaces.
abarnert
source share