Skip to content

Commit

Permalink
Remove double warning about missing psutil on experiment start
Browse files Browse the repository at this point in the history
  • Loading branch information
pitercl committed Apr 1, 2020
1 parent beea197 commit 6bcfb4f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 62 deletions.
51 changes: 17 additions & 34 deletions neptune/internal/abort.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
import logging

_logger = logging.getLogger(__name__)
try:
import psutil
PSUTIL_INSTALLED = True
except ImportError:
PSUTIL_INSTALLED = False


class CustomAbortImpl(object):
Expand All @@ -34,56 +37,36 @@ def __init__(self, pid):

@staticmethod
def requirements_installed():
# pylint:disable=unused-import,unused-variable,bad-option-value,import-outside-toplevel
try:
import psutil
return True
except ImportError:
_logger.warning('psutil is not installed. The experiment will not be abortable.')
return False
return PSUTIL_INSTALLED

def abort(self):
# pylint:disable=bad-option-value,import-outside-toplevel
import psutil

process = None
try:
process = psutil.Process(self._pid)
processes = self._get_process_with_children(psutil.Process(self._pid))
except psutil.NoSuchProcess:
pass
processes = []

if process is not None:
processes = self._get_processes(process)
for p in processes:
self._abort(p)
_, alive = psutil.wait_procs(processes, timeout=self.KILL_TIMEOUT)
for p in alive:
self._kill(p)
for p in processes:
self._abort(p)
_, alive = psutil.wait_procs(processes, timeout=self.KILL_TIMEOUT)
for p in alive:
self._kill(p)

@staticmethod
def _get_processes(process):
# pylint:disable=bad-option-value,import-outside-toplevel
import psutil

def _get_process_with_children(process):
try:
return [process] + process.children(recursive=True)
except psutil.NoSuchProcess:
return []

def _abort(self, process):
# pylint:disable=bad-option-value,import-outside-toplevel
import psutil

@staticmethod
def _abort(process):
try:
process.terminate()
except psutil.NoSuchProcess:
pass

def _kill(self, process):
# pylint:disable=bad-option-value,import-outside-toplevel
import psutil

for process in self._get_processes(process):
for process in self._get_process_with_children(process):
try:
if process.is_running():
process.kill()
Expand Down
22 changes: 15 additions & 7 deletions neptune/internal/execution/execution_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import sys
import time
import traceback
import logging
from logging import StreamHandler

from neptune.internal.abort import DefaultAbortImpl, CustomAbortImpl
Expand All @@ -33,6 +34,9 @@
from neptune.utils import is_notebook, in_docker


_logger = logging.getLogger(__name__)


class ExecutionContext(object):

def __init__(self, backend, experiment):
Expand Down Expand Up @@ -61,11 +65,6 @@ def start(self,
if handle_uncaught_exceptions:
self._set_uncaught_exception_handler()

abortable = abort_callback is not None or DefaultAbortImpl.requirements_installed()

if abortable:
self._run_aborting_thread(abort_callback)

if logger:
# pylint: disable=protected-access
channel = self._experiment._get_channel('logger', 'text', ChannelNamespace.SYSTEM)
Expand All @@ -80,11 +79,20 @@ def start(self,
if upload_stderr and not is_notebook():
self._stderr_uploader = StdErrWithUpload(self._experiment)

abortable = abort_callback is not None or DefaultAbortImpl.requirements_installed()
if abortable:
self._run_aborting_thread(abort_callback)
else:
_logger.warning('psutil is not installed. You will not be able to abort this experiment from the UI.')

if run_monitoring_thread:
self._run_monitoring_thread()

if send_hardware_metrics and SystemMonitor.requirements_installed():
self._run_hardware_metrics_reporting_thread()
if send_hardware_metrics:
if SystemMonitor.requirements_installed():
self._run_hardware_metrics_reporting_thread()
else:
_logger.warning('psutil is not installed. Hardware metrics will not be collected.')

def stop(self):
if self._ping_thread:
Expand Down
36 changes: 15 additions & 21 deletions neptune/internal/hardware/system/system_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,27 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
import logging

_logger = logging.getLogger(__name__)
try:
import psutil
PSUTIL_INSTALLED = True
except ImportError:
PSUTIL_INSTALLED = False


class SystemMonitor(object):
@staticmethod
def requirements_installed():
# pylint:disable=unused-import,unused-variable,bad-option-value,import-outside-toplevel
try:
import psutil
return True
except ImportError:
_logger.warning('psutil is not installed. Hardware metrics will not be collected.')
return False
return PSUTIL_INSTALLED

def cpu_count(self):
return self._psutil().cpu_count()

def cpu_percent(self):
return self._psutil().cpu_percent()
@staticmethod
def cpu_count():
return psutil.cpu_count()

def virtual_memory(self):
return self._psutil().virtual_memory()
@staticmethod
def cpu_percent():
return psutil.cpu_percent()

@classmethod
def _psutil(cls):
# pylint:disable=bad-option-value,import-outside-toplevel
import psutil
return psutil
@staticmethod
def virtual_memory():
return psutil.virtual_memory()
1 change: 1 addition & 0 deletions neptune/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ def glob(pathname):
else:
return globlib.glob(pathname, recursive=True)


def is_ipython():
try:
# pylint:disable=bad-option-value,import-outside-toplevel
Expand Down

0 comments on commit 6bcfb4f

Please sign in to comment.