Skip to content

Commit

Permalink
Remove conditional imports of psutil (#7462)
Browse files Browse the repository at this point in the history
  • Loading branch information
crusaderky authored Jan 9, 2023
1 parent 3e793f7 commit 9f72645
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 19 deletions.
10 changes: 3 additions & 7 deletions distributed/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from collections.abc import Callable
from functools import wraps

import psutil

from distributed.compatibility import WINDOWS

_empty_namedtuple = collections.namedtuple("_empty_namedtuple", ())
Expand All @@ -13,14 +15,8 @@
def _psutil_caller(method_name, default=_empty_namedtuple):
"""
Return a function calling the given psutil *method_name*,
or returning *default* if psutil is not present.
or returning *default* if psutil fails.
"""
# Import only once to avoid the cost of a failing import at each wrapper() call
try:
import psutil
except ImportError: # pragma: no cover
return default

meth = getattr(psutil, method_name)

@wraps(meth)
Expand Down
3 changes: 1 addition & 2 deletions distributed/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from typing import ClassVar, Iterator, TypeVar, overload

import click
import psutil
import tblib.pickling_support

try:
Expand Down Expand Up @@ -200,8 +201,6 @@ def get_ip_interface(ifname):
ValueError is raised if the interface does no have an IPv4 address
associated with it.
"""
import psutil

net_if_addrs = psutil.net_if_addrs()

if ifname not in net_if_addrs:
Expand Down
14 changes: 4 additions & 10 deletions distributed/utils_perf.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import threading
from collections import deque

import psutil

from dask.utils import format_bytes

from distributed.metrics import thread_time
Expand Down Expand Up @@ -147,12 +149,7 @@ def __init__(self, warn_over_frac=0.1, info_over_rss_win=10 * 1e6):
def enable(self):
assert not self._enabled
self._fractional_timer = FractionalTimer(n_samples=self.N_SAMPLES)
try:
import psutil
except ImportError:
self._proc = None
else:
self._proc = psutil.Process()
self._proc = psutil.Process()

cb = self._gc_callback
assert cb not in gc.callbacks
Expand Down Expand Up @@ -181,10 +178,7 @@ def _gc_callback(self, phase, info):
# don't waste time measuring them
if info["generation"] != 2:
return
if self._proc is not None:
rss = self._proc.memory_info().rss
else:
rss = 0
rss = self._proc.memory_info().rss
if phase == "start":
self._fractional_timer.start_timing()
self._gc_rss_before = rss
Expand Down

0 comments on commit 9f72645

Please sign in to comment.