Browse Source

Start thread management stuff

tags/nilmrun-0.2
Jim Paris 7 years ago
parent
commit
a9bac7d9a0
4 changed files with 72 additions and 6 deletions
  1. +10
    -0
      .coveragerc
  2. +4
    -3
      data.js
  3. +47
    -3
      src/server.py
  4. +11
    -0
      src/threadmanager.py

+ 10
- 0
.coveragerc View File

@@ -0,0 +1,10 @@
# -*- conf -*-

[run]
# branch = True

[report]
exclude_lines =
pragma: no cover
if 0:
omit = scripts,src/_version.py

+ 4
- 3
data.js View File

@@ -12,8 +12,7 @@
"start": 1366260494269078,
"end": 1366260608185031,
"columns": [ { "name": "P1", "index": 0 },
{ "name": "Q1", "index": 1 },
{ "name": "P3", "index": 2 }
{ "name": "Q1", "index": 1 }
]
},
{ "name": "Boiler Pump OFF",
@@ -21,7 +20,9 @@
"stream": "/sharon/prep-a",
"start": 1366260864215764,
"end": 1366260870882998,
"columns": [ { "name": "P1", "index": 0 } ]
"columns": [ { "name": "P1", "index": 0 },
{ "name": "Q1", "index": 1 }
]
}
]
}

+ 47
- 3
src/server.py View File

@@ -30,7 +30,7 @@ import nilmrun.trainola
cherrypy.tools.CORS_allow = cherrypy.Tool('on_start_resource', CORS_allow)

# CherryPy apps
class NilmRunApp(object):
class App(object):
"""Root application for NILM runner"""

def __init__(self):
@@ -55,7 +55,49 @@ class NilmRunApp(object):
def version(self):
return nilmrun.__version__

# /trainola
class AppThread(object):

def __init__(self, manager):
self.manager = manager

def thread_status(self, pid):
return {
"pid": pid,
"alive": self.manager[pid].alive,
"name": self.manager[pid].name,
"start_time": self.manager[pid].start_time,
"parameters": self.manager[pid].parameters,
"log": self.manager[pid].log,
}

# /thread/status
@cherrypy.expose
@cherrypy.tools.json_out()
def status(self, pid, clear = False):
if pid not in self.manager:
raise cherrypy.NotFound()
status = thread_status(pid)
if clear:
self.manager[pid].clear_log()
return status

# /thread/kill
@cherrypy.expose
@cherrypy.tools.json_in()
@cherrypy.tools.json_out()
@cherrypy.tools.CORS_allow(methods = ["POST"])
def kill(self, pid):
if pid not in self.manager:
raise CherryPy.NotFound()
if not self.manager.terminate(pid):
raise cherrypy.HTTPError("503 Service Unavailable",
"Failed to stop thread")
status = thread_status(pid)
manager.remove(pid)

class AppFilter(object):

# /filter/trainola
@cherrypy.expose
@cherrypy.tools.json_in()
@cherrypy.tools.json_out()
@@ -114,7 +156,9 @@ class Server(object):
cherrypy._cperror._ie_friendly_error_sizes = {}

# Build up the application and mount it
root = NilmRunApp()
root = App()
root.thread = AppThread()
root.filter = AppFilter()
cherrypy.tree.apps = {}
cherrypy.tree.mount(root, basepath, config = { "/" : app_config })



+ 11
- 0
src/threadmanager.py View File

@@ -0,0 +1,11 @@
#!/usr/bin/python

from nilmdb.utils.printf import *

import threading

class ThreadManager(object):
def __init__(self):
self.threads = {}


Loading…
Cancel
Save