Skip to content

Commit

Permalink
Use scandir to save memory
Browse files Browse the repository at this point in the history
  • Loading branch information
QuantumEnergyE committed Dec 24, 2018
1 parent edba525 commit e86bf26
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/watchdog/observers/polling.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
"""

from __future__ import with_statement
import os
import threading
from functools import partial
from watchdog.utils import stat as default_stat
Expand All @@ -58,6 +57,11 @@
FileModifiedEvent
)

try:
from os import scandir
except ImportError:
from os import listdir as scandir


class PollingEmitter(EventEmitter):
"""
Expand All @@ -66,7 +70,7 @@ class PollingEmitter(EventEmitter):
"""

def __init__(self, event_queue, watch, timeout=DEFAULT_EMITTER_TIMEOUT,
stat=default_stat, listdir=os.listdir):
stat=default_stat, listdir=scandir):
EventEmitter.__init__(self, event_queue, watch, timeout)
self._snapshot = None
self._lock = threading.Lock()
Expand Down
11 changes: 8 additions & 3 deletions src/watchdog/utils/dirsnapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
import os
from stat import S_ISDIR
from watchdog.utils import stat as default_stat
try:
from os import scandir
except ImportError:
from os import listdir as scandir


class DirectorySnapshotDiff(object):
Expand Down Expand Up @@ -193,13 +197,13 @@ class DirectorySnapshot(object):
A function with the signature ``walker_callback(path, stat_info)``
which will be called for every entry in the directory tree.
:param listdir:
Use custom listdir function. See ``os.listdir`` for details.
Use custom listdir function. For details see ``os.scandir`` if available, else ``os.listdir``.
"""

def __init__(self, path, recursive=True,
walker_callback=(lambda p, s: None),
stat=default_stat,
listdir=os.listdir):
listdir=scandir):
self._stat_info = {}
self._inode_to_path = {}

Expand All @@ -209,7 +213,8 @@ def __init__(self, path, recursive=True,

def walk(root):
try:
paths = [os.path.join(root, name) for name in listdir(root)]
paths = [os.path.join(root, entry if isinstance(entry, str) else entry.name)
for entry in listdir(root)]
except OSError as e:
# Directory may have been deleted between finding it in the directory
# list of its parent and trying to delete its contents. If this
Expand Down

0 comments on commit e86bf26

Please sign in to comment.