Python - 如何检查文件是否被另一个应用程序使用? [英] Python - How to check if a file is used by another application?

查看:44
本文介绍了Python - 如何检查文件是否被另一个应用程序使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打开一个由另一个应用程序定期写入的文件.此应用程序无法修改.因此,我只想在我知道文件未被其他应用程序写入时才打开该文件.

I want to open a file which is periodically written to by another application. This application cannot be modified. I'd therefore like to only open the file when I know it is not been written to by an other application.

有没有pythonic的方法来做到这一点?否则,我如何在 Unix 和 Windows 中实现这一点?

Is there a pythonic way to do this? Otherwise, how do I achieve this in Unix and Windows?

编辑:我会尽力澄清.有没有办法检查当前文件是否已被另一个应用程序打开?

我想从这个问题开始.这些其他应用程序是否读/写暂时无关紧要.

I'd like to start with this question. Whether those other application read/write is irrelevant for now.

我意识到它可能依赖于操作系统,所以现在这可能与 python 无关.

I realize it is probably OS dependent, so this may not really be python related right now.

推荐答案

您的 Python 脚本是否希望打开文件进行写入或读取?遗留应用程序是在写入之间打开和关闭文件,还是让它保持打开状态?

Will your python script desire to open the file for writing or for reading? Is the legacy application opening and closing the file between writes, or does it keep it open?

了解遗留应用程序在做什么以及您的 Python 脚本试图实现什么非常重要.

It is extremely important that we understand what the legacy application is doing, and what your python script is attempting to achieve.

这一功能领域高度依赖于操作系统,不幸的是,您无法控制旧应用程序这一事实只会让事情变得更加困难.是否有 Pythonic 或非 Pythonic 的方式来执行此操作可能是您最不关心的问题 - 困难的问题是您想要实现的目标是否完全可能.

This area of functionality is highly OS-dependent, and the fact that you have no control over the legacy application only makes things harder unfortunately. Whether there is a pythonic or non-pythonic way of doing this will probably be the least of your concerns - the hard question will be whether what you are trying to achieve will be possible at all.

更新

好的,所以知道(从您的评论中):

OK, so knowing (from your comment) that:

旧应用程序正在打开并且每 X 分钟关闭一次文件,但是我不想假设在 t =t_0 + n*X + eps 它已经关闭文件.

the legacy application is opening and closing the file every X minutes, but I do not want to assume that at t = t_0 + n*X + eps it already closed the file.

然后更改问题的参数.考虑到一些假设,它实际上可以以独立于操作系统的方式完成,或者作为依赖于操作系统和独立于操作系统的技术的组合.:)

then the problem's parameters are changed. It can actually be done in an OS-independent way given a few assumptions, or as a combination of OS-dependent and OS-independent techniques. :)

  1. 独立于操作系统的方式:如果可以安全地假设遗留应用程序最多将文件保持打开某个已知时间量,例如 T 秒(例如打开文件,执行一次写入,然后关闭文件),并或多或少每隔 X 秒重新打开它,其中 X 大于 2*.
    • stat 文件
    • now()中减去文件的修改时间,得到D
    • if T <= D <X 然后打开文件并执行您需要的操作
    • 这对于您的应用程序来说可能足够安全.安全性随着 T/X 的降低而增加.在 *nix 上,您可能需要仔细检查 /etc/ntpd.conf 以获取正确的时间步进与回转配置(请参阅 tinker).对于 Windows,请参阅 MSDN
  1. OS-independent way: if it is safe to assume that the legacy application keeps the file open for at most some known quantity of time, say T seconds (e.g. opens the file, performs one write, then closes the file), and re-opens it more or less every X seconds, where X is larger than 2*T.
    • stat the file
    • subtract file's modification time from now(), yielding D
    • if T <= D < X then open the file and do what you need with it
    • This may be safe enough for your application. Safety increases as T/X decreases. On *nix you may have to double check /etc/ntpd.conf for proper time-stepping vs. slew configuration (see tinker). For Windows see MSDN
  • 共享(锁定):这假设旧程序也以共享模式打开文件(通常是 Windows 应用程序中的默认设置);此外,如果您的应用程序在旧应用程序尝试相同(竞争条件)时获取锁,则旧应用程序将失败.
    • 这是非常具有侵入性且容易出错的.除非新应用程序和旧应用程序都需要同步访问以写入同一文件,并且您愿意处理旧应用程序被拒绝打开文件的可能性,否则不要使用此方法.
    • 与独立于操作系统的技术相比,您更容易受到竞争条件的影响

    /fd/ 指向哪个文件

    • 与独立于操作系统的技术相比,您更容易受到竞争条件的影响
    • 遗留应用程序极不可能使用锁定,但如果是,锁定就不是一个真正的选择,除非遗留应用程序可以优雅地处理锁定的文件(通过阻塞,而不是失败——并且如果您自己的应用程序可以保证文件将不会保持锁定状态,从而在更长的时间内阻止旧应用程序.)
      • you are even more vulnerable to race conditions than the OS-independent technique
      • it is highly unlikely that the legacy application uses locking, but if it is, locking is not a real option unless the legacy application can handle a locked file gracefully (by blocking, not by failing - and if your own application can guarantee that the file will not remain locked, blocking the legacy application for extender periods of time.)

      <小时>

      更新 2

      如果支持检查遗留应用程序是否打开了文件"(容易出现竞争条件的侵入性方法),那么您可以通过以下方式解决所述竞争条件:

      If favouring the "check whether the legacy application has the file open" (intrusive approach prone to race conditions) then you can solve the said race condition by:

      1. 检查遗留应用程序是否打开了文件(a la lsofProcessExplorer)
      2. 暂停旧的申请流程
      3. 重复步骤 1 中的检查以确认旧应用程序未在步骤 1 和 2 之间打开文件;如果是,则延迟并重新启动步骤 1,否则进行步骤 4
      4. 处理您的文件 - 理想情况下只需将其重命名以进行后续独立处理,以便将旧应用程序暂停最少的时间
      5. 恢复旧的申请流程

      这篇关于Python - 如何检查文件是否被另一个应用程序使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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