Implement timeout in windows filecopy

喜欢而已 提交于 2019-12-13 08:00:02

问题


I want to copy a list of files on windows using python. When doing that manually, I see timeouts in some files, therefore the copy process fails. I need a way to implement the timeout check in python.

So far I use the win32 API:

import win32file
files = {'source_a' : 'dest_a', 'source_b' : 'dest_b'}

for f in files.keys():
    win32file.CopyFileW(f,files[f],0)

In some cases, the CopyFileW function doesn't return in "reasonable time", for the sake of this discussion, let's say 5 seconds. How can I wrap this function to be checked against a timer?

Edit: As suggested, I switched over to using the CopyFileEx function, because it has a cancellation Interface. If I put a timeout check in the callback function, the copy process is stalled as long as the callback is running. As I understand, the calls to the callback function only are issued, when there IS some file copying activity. If there ISN'T for a longer time, I don't see that.


回答1:


Instead of CopyFile, use CopyFileEx which provides an interface that supports cancellation.



来源:https://stackoverflow.com/questions/42063906/implement-timeout-in-windows-filecopy

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