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 22, 2018
1 parent edba525 commit 12076b8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/watchdog/observers/polling.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@
FileModifiedEvent
)

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


class PollingEmitter(EventEmitter):
"""
Expand All @@ -66,7 +71,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
9 changes: 7 additions & 2 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 @@ -199,7 +203,7 @@ class DirectorySnapshot(object):
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 12076b8

Please sign in to comment.