Skip to content

Commit

Permalink
adding awatch lock
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin committed Oct 18, 2017
1 parent f26a86e commit b2891c1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
29 changes: 16 additions & 13 deletions watchgod/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import signal
import sys
from asyncio import Lock as AsyncLock
from functools import partial
from multiprocessing import Process
from pathlib import Path
Expand Down Expand Up @@ -56,7 +57,7 @@ class awatch:
3.5 doesn't support yield in coroutines so we need all this fluff. Yawwwwn.
"""
__slots__ = '_loop', '_path', '_watcher_cls', '_debounce', '_min_sleep', '_start', '_w'
__slots__ = '_loop', '_path', '_watcher_cls', '_debounce', '_min_sleep', '_start', '_w', 'lock'

def __init__(self, path: Union[Path, str], *,
watcher_cls: Type[AllWatcher]=DefaultWatcher,
Expand All @@ -69,6 +70,7 @@ def __init__(self, path: Union[Path, str], *,
self._min_sleep = min_sleep
self._start = 0
self._w = None
self.lock = AsyncLock()

@correct_aiter
def __aiter__(self):
Expand All @@ -78,18 +80,19 @@ async def __anext__(self):
if not self._w:
self._w = await self._loop.run_in_executor(None, self._watcher_cls, self._path)
while True:
if self._start:
sleep_time = max(self._debounce - (unix_ms() - self._start), self._min_sleep)
await asyncio.sleep(sleep_time / 1000)

self._start = unix_ms()
changes = await self._loop.run_in_executor(None, self._w.check)
if logger.isEnabledFor(logging.DEBUG):
logger.debug('time=%0.0fms files=%d changes=%d',
unix_ms() - self._start, len(self._w.files), len(changes))

if changes:
return changes
async with self.lock:
if self._start:
sleep_time = max(self._debounce - (unix_ms() - self._start), self._min_sleep)
await asyncio.sleep(sleep_time / 1000)

self._start = unix_ms()
changes = await self._loop.run_in_executor(None, self._w.check)
if logger.isEnabledFor(logging.DEBUG):
logger.debug('time=%0.0fms files=%d changes=%d',
unix_ms() - self._start, len(self._w.files), len(changes))

if changes:
return changes


def _start_process(target, args, kwargs):
Expand Down
2 changes: 1 addition & 1 deletion watchgod/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

__all__ = ['VERSION']

VERSION = StrictVersion('0.0.1')
VERSION = StrictVersion('0.0.2')

0 comments on commit b2891c1

Please sign in to comment.