Skip to content

Instantly share code, notes, and snippets.

@tiagocoutinho
Last active October 2, 2019 05:37
Show Gist options
  • Save tiagocoutinho/1bcbb66e9753b037876b184b107aa96d to your computer and use it in GitHub Desktop.
Save tiagocoutinho/1bcbb66e9753b037876b184b107aa96d to your computer and use it in GitHub Desktop.
gevent friendly inotify
"""
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