Last active
October 2, 2019 05:37
-
-
Save tiagocoutinho/1bcbb66e9753b037876b184b107aa96d to your computer and use it in GitHub Desktop.
gevent friendly inotify
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
A gevent friendly version of inotify | |
Either instanciate Inotify (or InotifyTree) objects directly from `ginotify.Inotify`: | |
```python | |
from ginotify import Inotify | |
ify1 = Inotify() | |
ify1.add_watch('/tmp') | |
``` | |
...or instanciate `inotify.adapters.Inotify` objects directly by ing them with | |
`ginotify.ensure` context manager: | |
```python | |
from ginotify import ensure | |
from inotify.adapters import Inotify | |
with ensure(): | |
ify1 = Inotify() | |
ify1.add_watch('/tmp') | |
``` | |
""" | |
import contextlib | |
import gevent.select | |
import inotify.adapters | |
@contextlib.contextmanager | |
def ensure(): | |
epoll = inotify.adapters.select.epoll | |
inotify.adapters.select.epoll = gevent.select.poll | |
yield | |
inotify.adapters.select.epoll = epoll | |
class Inotify(inotify.adapters.Inotify): | |
def __init__(self, *args, **kwargs): | |
with ensure(): | |
super(Inotify, self).__init__(*args, **kwargs) | |
class InotifyTree(inotify.adapters.InotifyTree): | |
def __init__(self, *args, **kwargs): | |
with ensure(): | |
super(InotifyTree, self).__init__(*args, **kwargs) | |
class InotifyTrees(inotify.adapters.InotifyTrees): | |
def __init__(self, *args, **kwargs): | |
with ensure(): | |
super(InotifyTrees, self).__init__(*args, **kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment