Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use scandir to save memory #503

Merged
merged 1 commit into from
Dec 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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