Browse Source

Update for Python 3 and psutil >= 2.

tags/nilmrun-2.0.0^0
Jim Paris 3 years ago
parent
commit
cd68389e9a
3 changed files with 22 additions and 23 deletions
  1. +13
    -15
      nilmrun/processmanager.py
  2. +2
    -1
      requirements.txt
  3. +7
    -7
      tests/test_nilmrun.py

+ 13
- 15
nilmrun/processmanager.py View File

@@ -23,7 +23,7 @@ class LogReceiver(object):
and stores them locally."""
def __init__(self, pipe):
self.pipe = pipe
self.log = io.StringIO()
self.log = io.BytesIO()
self.thread = threading.Thread(target = self.run)
self.thread.start()

@@ -39,7 +39,7 @@ class LogReceiver(object):
return self.log.getvalue()

def clear(self):
self.log = io.StringIO()
self.log = io.BytesIO()

class Process(object):
"""Spawn and manage a subprocess, and capture its output."""
@@ -101,7 +101,7 @@ class Process(object):
# Find all children
group = getpgid(self._process.pid)
main = psutil.Process(self._process.pid)
allproc = [ main ] + main.get_children(recursive = True)
allproc = [ main ] + main.children(recursive = True)

# Kill with SIGTERM, if they're still in this process group
for proc in allproc:
@@ -142,9 +142,9 @@ class Process(object):
Call .get_info() about a second later."""
try:
main = psutil.Process(self._process.pid)
self._process_list = [ main ] + main.get_children(recursive = True)
self._process_list = [ main ] + main.children(recursive = True)
for proc in self._process_list:
proc.get_cpu_percent(0)
proc.cpu_percent(0)
except psutil.Error: # pragma: no cover (race condition)
self._process_list = [ ]

@@ -165,14 +165,14 @@ class Process(object):
d = self.get_empty_info()
for proc in self._process_list:
try:
d["cpu_percent"] += proc.get_cpu_percent(0)
cpuinfo = proc.get_cpu_times()
d["cpu_percent"] += proc.cpu_percent(0)
cpuinfo = proc.cpu_times()
d["cpu_user"] += cpuinfo.user
d["cpu_sys"] += cpuinfo.system
meminfo = proc.get_memory_info()
meminfo = proc.memory_info()
d["mem_phys"] += meminfo.rss
d["mem_virt"] += meminfo.vms
ioinfo = proc.get_io_counters()
ioinfo = proc.io_counters()
d["io_read"] += ioinfo.read_bytes
d["io_write"] += ioinfo.write_bytes
d["procs"] += 1
@@ -275,13 +275,11 @@ class ProcessManager(object):

# Retrieve info for system
info["system"]["cpu_percent"] = sum(psutil.cpu_percent(0, percpu=True))
info["system"]["cpu_max"] = 100.0 * psutil.NUM_CPUS
info["system"]["procs"] = len(psutil.get_pid_list())
# psutil > 0.6.0's psutil.virtual_memory() would be better here,
# but this should give the same info.
meminfo = psutil.phymem_usage()
info["system"]["cpu_max"] = 100.0 * psutil.cpu_count()
info["system"]["procs"] = len(psutil.pids())
meminfo = psutil.virtual_memory()
info["system"]["mem_total"] = meminfo.total
info["system"]["mem_used"] = int(meminfo.total * meminfo.percent / 100)
info["system"]["mem_used"] = meminfo.used

# Retrieve info for each PID
for pid in self:


+ 2
- 1
requirements.txt View File

@@ -1 +1,2 @@
nilmdb>=2.0.0
nilmdb>=2.0.2
psutil>=2.0.0

+ 7
- 7
tests/test_nilmrun.py View File

@@ -287,9 +287,9 @@ class TestClient(object):

# basic code snippet
code = textwrap.dedent("""
print 'hello'
print('hello')
def foo(arg):
print 'world'
print('world')
""")
status = do(code, [], False)
eq_("hello\n", status["log"])
@@ -298,7 +298,7 @@ class TestClient(object):
# compile error
code = textwrap.dedent("""
def foo(arg:
print 'hello'
print('hello')
""")
status = do(code, [], False)
in_("SyntaxError", status["log"])
@@ -323,7 +323,7 @@ class TestClient(object):
# argument handling (strings come in as unicode)
code = textwrap.dedent("""
import sys
print sys.argv[1].encode('ascii'), sys.argv[2]
print(sys.argv[1], sys.argv[2])
sys.exit(0) # also test raising SystemExit
""")
with assert_raises(ClientError) as e:
@@ -336,9 +336,9 @@ class TestClient(object):
# try killing a long-running process
code = textwrap.dedent("""
import time
print 'hello'
print('hello')
time.sleep(60)
print 'world'
print('world')
""")
status = do(code, [], True)
eq_(status["log"], "hello\n")
@@ -347,7 +347,7 @@ class TestClient(object):
# default arguments are empty
code = textwrap.dedent("""
import sys
print 'args:', len(sys.argv[1:])
print('args:', len(sys.argv[1:]))
""")
status = do(code, None, False)
eq_(status["log"], "args: 0\n")


Loading…
Cancel
Save