You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
*inotify* functionality is available from the Linux kernel and allows you to register one or more directories for watching, and to simply block and wait for notification events. This is obviously far more efficient than polling one or more directories to determine if anything has changed. This is available in the Linux kernel as of version 2.6 .
9
6
@@ -12,105 +9,103 @@ We've designed this library to act as a generator. All you have to do is loop, a
12
9
**This project is unrelated to the *PyInotify* project that existed prior to this one (this project began in 2015). That project is defunct and no longer available.**
13
10
14
11
15
-
==========
16
-
Installing
17
-
==========
12
+
# Installing
18
13
19
-
Install via *pip*::
14
+
Install via *pip*:
20
15
21
-
$ sudo pip install inotify
16
+
```
17
+
$ sudo pip install inotify
18
+
```
22
19
23
20
24
-
=======
25
-
Example
26
-
=======
21
+
# Example
27
22
28
23
Code for monitoring a simple, flat path (see "Recursive Watching" for watching a hierarchical structure):
File "/home/dustin/development/python/pyinotify/inotify/adapters.py", line 202, in event_gen
56
+
events = self.__epoll.poll(block_duration_s)
57
+
KeyboardInterrupt
58
+
```
64
59
65
60
Note that this works quite nicely, but, in the event that you don't want to be driven by the loop, you can also provide a timeout and then even flatten the output of the generator directly to a list:
**Note that the event-loop will automatically register new directories to be watched, so, if you will create new directories and then potentially delete them, between calls, and are only retrieving the events in batches (like above) then you might experience issues. See the parameters for `event_gen()` for options to handle this scenario.**
96
93
97
94
98
-
==================
99
-
Recursive Watching
100
-
==================
95
+
# Recursive Watching
101
96
102
97
There is also the ability to add a recursive watch on a path.
103
98
104
99
Example:
105
100
106
-
.. code-block:: python
101
+
```python
102
+
i = inotify.adapters.InotifyTree('/tmp/watch_tree')
107
103
108
-
i = inotify.adapters.InotifyTree('/tmp/watch_tree')
104
+
for event in i.event_gen():
105
+
# Do stuff...
109
106
110
-
for event in i.event_gen():
111
-
# Do stuff...
112
-
113
-
pass
107
+
pass
108
+
```
114
109
115
110
This will immediately recurse through the directory tree and add watches on all subdirectories. New directories will automatically have watches added for them and deleted directories will be cleaned-up.
116
111
@@ -120,9 +115,7 @@ The other differences from the standard functionality:
120
115
- Even if you provide a very restrictive mask that doesn't allow for directory create/delete events, the *IN_ISDIR*, *IN_CREATE*, and *IN_DELETE* flags will still be seen.
121
116
122
117
123
-
=====
124
-
Notes
125
-
=====
118
+
# Notes
126
119
127
120
- **IMPORTANT:** Recursively monitoring paths is **not** a functionality provided by the kernel. Rather, we artificially implement it. As directory-created events are received, we create watches for the child directories on-the-fly. This means that there is potential for a race condition: if a directory is created and a file or directory is created inside before you (using the `event_gen()` loop) have a chance to observe it, then you are going to have a problem: If it is a file, then you will miss the events related to its creation, but, if it is a directory, then not only will you miss those creation events but this library will also miss them and not be able to add a watch for them. If you are dealing with a **large number of hierarchical directory creations** and have the ability to be aware new directories via a secondary channel with some lead time before any files are populated *into* them, you can take advantage of this and call `add_watch()` manually. In this case there is limited value in using `InotifyTree()`/`InotifyTree()` instead of just `Inotify()` but this choice is left to you.
128
121
@@ -135,16 +128,11 @@ Notes
135
128
- Calling `remove_watch()` is not strictly necessary. The *inotify* resources is automatically cleaned-up, which would clean-up all watch resources as well.
136
129
137
130
138
-
=======
139
-
Testing
140
-
=======
141
-
142
-
Install the testing dependencies and use nose2 to run the tests::
131
+
# Testing
143
132
144
-
$ pip install -r requirements-testing.txt
145
-
$ ./test.sh
133
+
Install the testing dependencies and use nose2 to run the tests:
0 commit comments