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

Import psutil conditionally; install it only in Linux platforms #103

Merged
merged 1 commit into from
May 11, 2021
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
5 changes: 4 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ def main():

python_requires='>=3.6', # supported Python ranges
install_requires=[
'psutil',
# psutil not supported on Windows, we haven't tested in other platforms, but since it's not essential
# to the functioning of Tarski, better be conservative here and install only on Linux.
'psutil; platform_system=="Linux"',

'multipledispatch',

# Antlr pinned to a specific version to avoid messages "ANTLR runtime and generated code versions disagree"
Expand Down
25 changes: 20 additions & 5 deletions src/tarski/utils/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,39 @@
import os
import sys
import time
import psutil


def get_mem_usage():
""" Return the memory usage as reported by psutil, or None, if the platform
does not support psutil, or """
try:
import psutil
except ImportError:
return None
return psutil.Process().memory_info().rss


class Timer:
def __init__(self):
self.start_time = time.time()
self.start_clock = self._clock()
self.start_mem = psutil.Process().memory_info().rss
self.start_mem = get_mem_usage()

@staticmethod
def _clock():
times = os.times()
return times[0] + times[1]

def __str__(self):
current = psutil.Process().memory_info().rss
current_in_mb = current / (1024*1024)
rss_in_mb = (current - self.start_mem) / (1024*1024)
rss_in_mb = "-"
current_in_mb = "-"
if self.start_mem is not None:
# self.start_mem is None whenever the underlying platform could not import the psutil module,
# in which case at the moment we're happy simply not to show memory consumption info.
current = get_mem_usage()
current_in_mb = current / (1024*1024)
rss_in_mb = (current - self.start_mem) / (1024*1024)

return "[%.2fs CPU, %.2fs wall-clock, diff: %.2fMB, curr: %.2fMB]" % (
self._clock() - self.start_clock,
time.time() - self.start_time, rss_in_mb, current_in_mb)
Expand Down
13 changes: 13 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

from tarski.utils import resources


def test_timer_class():
# Some very exciting algorithms to exercise the timing functionality
x = 0
with resources.timing("\tHello world"):
x += 1
assert x == 1
with resources.timing("\tHello world", newline=True):
x += 1
assert x == 2