简体   繁体   English

使用pyinotify来监视文件创建,但等待它完全写入磁盘

[英]Using pyinotify to watch for file creation, but waiting for it to be completely written to disk

I'm using pyinotify to watch a folder for when files are created in it. 我正在使用pyinotify来查看在其中创建文件的文件夹。 And when certain files are created I want to move them. 当创建某些文件时,我想移动它们。 The problem is that as soon as the file is created (obviously), my program tries to move it, even before it's completely written to disk. 问题是,一旦创建文件(显然),我的程序就会尝试移动它,甚至在它完全写入磁盘之前。

Is there a way to make pyinotify wait until a file is completely written to disk before notifying me that it's been created? 有没有办法使pyinotify等到一个文件完全写入磁盘,然后通知我它已被创建? Or is there any easy way to, after I'm notified, make python wait to move it until it's done being written? 还是有任何简单的方法,在我收到通知后,让python等待移动它直到它完成写入?

Have pyinotify react to IN_CLOSE_WRITE events: pyinotify对IN_CLOSE_WRITE事件做出反应:

wm.add_watch(watched_dir, pyinotify.IN_CLOSE_WRITE, proc_fun=MyProcessEvent())

This is from man 5 incrontab , but it applies equally well to pyinotify: 这是来自man 5 incrontab ,但它同样适用于pyinotify:

   IN_ACCESS           File was accessed (read) (*)
   IN_ATTRIB           Metadata changed (permissions, timestamps, extended attributes, etc.) (*)
   IN_CLOSE_WRITE      File opened for writing was closed (*)
   IN_CLOSE_NOWRITE    File not opened for writing was closed (*)
   IN_CREATE           File/directory created in watched directory (*)
   IN_DELETE           File/directory deleted from watched directory (*)
   IN_DELETE_SELF           Watched file/directory was itself deleted
   IN_MODIFY           File was modified (*)
   IN_MOVE_SELF        Watched file/directory was itself moved
   IN_MOVED_FROM       File moved out of watched directory (*)
   IN_MOVED_TO         File moved into watched directory (*)
   IN_OPEN             File was opened (*)

It is quite difficult to tell at this level if a file is being written to. 如果正在写入文件,则很难在此级别告诉。 What you can do is test to see if a file is opened by some other process. 你可以做的是测试一个文件是否被其他进程打开。

1) From the various flags that are used while opening a file, O_EXLOCK flag might be of help. 1)从打开文件时使用的各种标志,O_EXLOCK标志可能会有所帮助。 If the O_EXLOCK flag is set, the file descriptor has an exclusive lock on the file. 如果设置了O_EXLOCK标志,则文件描述符对文件具有独占锁定。 So my understanding is if you can do os.open() with O_EXLOCK flag, it's not open by other process. 所以我的理解是,如果你可以使用O_EXLOCK标志执行os.open(),它不会被其他进程打开。 This should work on all posix compatible OS but I have not tested it. 这适用于所有posix兼容的操作系统,但我还没有测试过。 If the file, is open then you could close, wait and retry again. 如果文件已打开,则可以关闭,等待并再次重试。

2) You can also try os.stat and see changing time stamp and try to safely interpret the information. 2)您也可以尝试os.stat并查看更改时间戳并尝试安全地解释信息。 Though this is not fool proof. 虽然这不是万无一失的。

3) On unix systems, you can try "lsof" 3)在unix系统上,你可以尝试“lsof”

4) The following page describes use of symlinks from /proc/PID/fd to test for open files 4)以下页面描述了使用/ proc / PID / fd中的符号链接来测试打开的文件

[Edit : Links updated] [编辑:链接更新]

如果您可以控制写入过程,则可以在写入文件时调用文件“foo.part”,并在关闭时将其重命名为“foo”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM