Skip to content

Instantly share code, notes, and snippets.

@tiagocoutinho
Created October 2, 2019 06:13
Show Gist options
  • Save tiagocoutinho/75061e9b6cd6435f0c658536a39d6f55 to your computer and use it in GitHub Desktop.
Save tiagocoutinho/75061e9b6cd6435f0c658536a39d6f55 to your computer and use it in GitHub Desktop.
gevent friendly intofy_simple
"""
A gevent friendly version of inotify_simple
Either instanciate INotify objects directly from `ginotify_simple.INotify`:
```python
from ginotify_simple import INotify, flags
ify1 = INotify()
watch = flags.CREATE | flags.DELETE | flags.MODIFY | flags.DELETE_SELF
ify1.add_watch('/tmp', watch)
```
...or instanciate `inotify_simple.INotify` objects directly by guarding them with
`ginotify_simple.ensure` context manager:
```python
from ginotify_simple import ensure
from inotify_simple import INotify
with ensure():
ify1 = INotify()
watch = flags.CREATE | flags.DELETE | flags.MODIFY | flags.DELETE_SELF
ify1.add_watch('/tmp', watch)
```
"""
import contextlib
import gevent.select
import inotify_simple
from inotify_simple import flags, masks, Event
@contextlib.contextmanager
def ensure():
select = inotify_simple.inotify_simple.select
inotify_simple.inotify_simple.select = gevent.select
yield
inotify_simple.inotify_simple.select = select
class INotify(inotify_simple.INotify):
def __init__(self, *args, **kwargs):
with ensure():
super(INotify, self).__init__(*args, **kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment