在 windows filecopy 中实现超时 [英] Implement timeout in windows filecopy

查看:49
本文介绍了在 windows filecopy 中实现超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 python 复制 Windows 上的文件列表.手动执行此操作时,我看到某些文件超时,因此复制过程失败.我需要一种在 python 中实现超时检查的方法.

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.

到目前为止,我使用的是 win32 API:

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)

在某些情况下,CopyFileW 函数不会在合理的时间"内返回,为了便于讨论,我们假设 5 秒.如何包装此函数以针对计时器进行检查?

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?

按照建议,我改用了 CopyFileEx 函数,因为它有一个取消接口.如果我在回调函数中进行超时检查,只要回调正在运行,复制过程就会停止.据我了解,只有在存在某些文件复制活动时才会发出对回调函数的调用.如果长时间没有,我就看不到了.

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.

推荐答案

代替 CopyFile,使用 CopyFileEx 提供了一个支持取消的接口.

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

这篇关于在 windows filecopy 中实现超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆