From e865fe53d277fcc95ffd5e7d3124223849b05613 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20K=C3=B6gl?= Date: Thu, 27 Mar 2014 08:44:24 +0100 Subject: [PATCH] changed ekgplotter osc thread to a process --- ekgplotter/ekgplotter/main.py | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/ekgplotter/ekgplotter/main.py b/ekgplotter/ekgplotter/main.py index a1143bf..47e921c 100644 --- a/ekgplotter/ekgplotter/main.py +++ b/ekgplotter/ekgplotter/main.py @@ -31,8 +31,7 @@ #gc.set_debug(gc.DEBUG_LEAK) from datetime import datetime -import threading -import queue +from queue import Empty import numpy as np import string,cgi,time, random, socket from os import curdir, sep @@ -55,6 +54,8 @@ from pyqtgraph.widgets.PlotWidget import PlotWidget from chaosc.argparser_groups import * from chaosc.lib import resolve_host +from multiprocessing import Process, Queue + try: from chaosc.c_osc_lib import * except ImportError: @@ -68,9 +69,10 @@ except ImportError as e: from chaosc.osc_lib import decode_osc -class OSCThread(threading.Thread): - def __init__(self, args): +class OSCThread(Process): + def __init__(self, args, queue): super(OSCThread, self).__init__() + self.msg_queue = queue self.args = args self.running = True @@ -127,20 +129,20 @@ class OSCThread(threading.Thread): if reads: osc_input = reads[0].recv(128) osc_address, typetags, messages = decode_osc(osc_input, 0, len(osc_input)) - #print("thread osc_address", osc_address) + print("thread: osc_address", osc_address) if osc_address.find(b"ekg") > -1 or osc_address.find(b"plot") != -1: #print("send", osc_address) - msg_queue.put_nowait((osc_address, messages)) + self.msg_queue.put_nowait((osc_address, messages)) else: - msg_queue.put_nowait((b"/bjoern/ekg", [0])) - msg_queue.put_nowait((b"/merle/ekg", [0])) - msg_queue.put_nowait((b"/uwe/ekg", [0])) + print("thread: dummy values") + self.msg_queue.put_nowait((b"/bjoern/ekg", [0])) + self.msg_queue.put_nowait((b"/merle/ekg", [0])) + self.msg_queue.put_nowait((b"/uwe/ekg", [0])) + time.sleep(0.01) self.unsubscribe_me() print("OSCThread is going down") -msg_queue = queue.Queue() - class Actor(object): shadowPen = pg.mkPen(255, 255, 255) brush = pg.mkBrush("w") @@ -333,8 +335,8 @@ class MyHandler(BaseHTTPRequestHandler): self.end_headers() self.wfile.write(data) elif self.path.endswith(".mjpeg"): - self.thread = thread = OSCThread(self.server.args) - thread.daemon = True + msg_queue = Queue() + self.thread = thread = OSCThread(self.server.args, msg_queue) thread.start() self.send_response(200) @@ -356,9 +358,10 @@ class MyHandler(BaseHTTPRequestHandler): while 1: try: osc_address, args = msg_queue.get_nowait() - except queue.Empty: + except Empty as e: + print("queue error", e, type(e)) break - + print("main: got value", osc_address, args) plotter.update(osc_address, args[0]) exporter = pg.exporters.ImageExporter.ImageExporter(plotter.plot.plotItem) @@ -421,7 +424,7 @@ class MyHandler(BaseHTTPRequestHandler): import traceback traceback.print_exc() # XXX But this goes to stderr! print( '-'*40) - self.send_error(404,'File Not Found: %s' % self.path) + self.send_error(404, 'File Not Found: %s' % self.path) class JustAHTTPServer(HTTPServer):