From: jcorgan Date: Fri, 10 Jul 2009 01:26:37 +0000 (+0000) Subject: Refactor msgq thread classes to use gru.msgq_runner X-Git-Url: https://git.gag.com/?a=commitdiff_plain;h=a9154607d6f6fd2bfbafd732dccf9edef35e1e6e;p=debian%2Fgnuradio Refactor msgq thread classes to use gru.msgq_runner git-svn-id: http://gnuradio.org/svn/gnuradio/trunk@11407 221aa14e-8319-0410-a670-987f0aec2ac5 --- diff --git a/gr-wxgui/src/python/common.py b/gr-wxgui/src/python/common.py index c6b9509b..d555a1f0 100644 --- a/gr-wxgui/src/python/common.py +++ b/gr-wxgui/src/python/common.py @@ -44,31 +44,26 @@ def register_access_methods(destination, controller): ################################################## # Input Watcher Thread ################################################## -import threading +from gnuradio import gru -class input_watcher(threading.Thread): +class input_watcher(gru.msgq_runner): """ Input watcher thread runs forever. Read messages from the message queue. Forward messages to the message handler. """ def __init__ (self, msgq, controller, msg_key, arg1_key='', arg2_key=''): - threading.Thread.__init__(self) - self.setDaemon(1) - self.msgq = msgq self._controller = controller self._msg_key = msg_key self._arg1_key = arg1_key self._arg2_key = arg2_key - self.keep_running = True - self.start() + gru.msgq_runner.__init__(self, msgq, self.handle_msg) + + def handle_msg(self, msg): + if self._arg1_key: self._controller[self._arg1_key] = msg.arg1() + if self._arg2_key: self._controller[self._arg2_key] = msg.arg2() + self._controller[self._msg_key] = msg.to_string() - def run(self): - while self.keep_running: - msg = self.msgq.delete_head() - if self._arg1_key: self._controller[self._arg1_key] = msg.arg1() - if self._arg2_key: self._controller[self._arg2_key] = msg.arg2() - self._controller[self._msg_key] = msg.to_string() ################################################## # Shared Functions diff --git a/gr-wxgui/src/python/fftsink_nongl.py b/gr-wxgui/src/python/fftsink_nongl.py index a1033e81..ca5e91fd 100644 --- a/gr-wxgui/src/python/fftsink_nongl.py +++ b/gr-wxgui/src/python/fftsink_nongl.py @@ -25,7 +25,6 @@ from gnuradio.wxgui import stdgui2 import wx import plot import numpy -import threading import math DIV_LEVELS = (1, 2, 5, 10, 20) @@ -193,34 +192,28 @@ class DataEvent(wx.PyEvent): self.__class__ (self.GetId()) -class input_watcher (threading.Thread): +class input_watcher (gru.msgq_runner): def __init__ (self, msgq, fft_size, event_receiver, **kwds): - threading.Thread.__init__ (self, **kwds) - self.setDaemon (1) - self.msgq = msgq self.fft_size = fft_size self.event_receiver = event_receiver - self.keep_running = True - self.start () - - def run (self): - while (self.keep_running): - msg = self.msgq.delete_head() # blocking read of message queue - itemsize = int(msg.arg1()) - nitems = int(msg.arg2()) - - s = msg.to_string() # get the body of the msg as a string - - # There may be more than one FFT frame in the message. - # If so, we take only the last one - if nitems > 1: - start = itemsize * (nitems - 1) - s = s[start:start+itemsize] - - complex_data = numpy.fromstring (s, numpy.float32) - de = DataEvent (complex_data) - wx.PostEvent (self.event_receiver, de) - del de + gru.msgq_runner.__init__(self, msgq, self.handle_msg) + + def handle_msg(self, msg): + itemsize = int(msg.arg1()) + nitems = int(msg.arg2()) + + s = msg.to_string() # get the body of the msg as a string + + # There may be more than one FFT frame in the message. + # If so, we take only the last one + if nitems > 1: + start = itemsize * (nitems - 1) + s = s[start:start+itemsize] + + complex_data = numpy.fromstring (s, numpy.float32) + de = DataEvent (complex_data) + wx.PostEvent (self.event_receiver, de) + del de class control_panel(wx.Panel): diff --git a/gr-wxgui/src/python/scopesink_nongl.py b/gr-wxgui/src/python/scopesink_nongl.py index bd318799..5c1379ee 100644 --- a/gr-wxgui/src/python/scopesink_nongl.py +++ b/gr-wxgui/src/python/scopesink_nongl.py @@ -25,7 +25,6 @@ from gnuradio.wxgui import stdgui2 import wx import gnuradio.wxgui.plot as plot import numpy -import threading import struct default_scopesink_size = (640, 240) @@ -193,48 +192,40 @@ class win_info (object): return self.marker -class input_watcher (threading.Thread): +class input_watcher (gru.msgq_runner): def __init__ (self, msgq, event_receiver, frame_decim, **kwds): - threading.Thread.__init__ (self, **kwds) - self.setDaemon (1) - self.msgq = msgq self.event_receiver = event_receiver self.frame_decim = frame_decim self.iscan = 0 - self.keep_running = True - self.start () - - def run (self): - # print "input_watcher: pid = ", os.getpid () - while (self.keep_running): - msg = self.msgq.delete_head() # blocking read of message queue - if self.iscan == 0: # only display at frame_decim - self.iscan = self.frame_decim - - nchan = int(msg.arg1()) # number of channels of data in msg - nsamples = int(msg.arg2()) # number of samples in each channel - - s = msg.to_string() # get the body of the msg as a string - - bytes_per_chan = nsamples * gr.sizeof_float - - records = [] - for ch in range (nchan): - - start = ch * bytes_per_chan - chan_data = s[start:start+bytes_per_chan] - rec = numpy.fromstring (chan_data, numpy.float32) - records.append (rec) + gru.msgq_runner.__init__(self, msgq, self.handle_msg) + def handle_msg(self, msg): + if self.iscan == 0: # only display at frame_decim + self.iscan = self.frame_decim + + nchan = int(msg.arg1()) # number of channels of data in msg + nsamples = int(msg.arg2()) # number of samples in each channel + + s = msg.to_string() # get the body of the msg as a string + + bytes_per_chan = nsamples * gr.sizeof_float + + records = [] + for ch in range (nchan): + + start = ch * bytes_per_chan + chan_data = s[start:start+bytes_per_chan] + rec = numpy.fromstring (chan_data, numpy.float32) + records.append (rec) + # print "nrecords = %d, reclen = %d" % (len (records),nsamples) - + de = DataEvent (records) wx.PostEvent (self.event_receiver, de) records = [] del de - # end if iscan == 0 - self.iscan -= 1 + self.iscan -= 1 class scope_window (wx.Panel): diff --git a/gr-wxgui/src/python/waterfallsink_nongl.py b/gr-wxgui/src/python/waterfallsink_nongl.py index 9d97c4e3..bb478c7c 100644 --- a/gr-wxgui/src/python/waterfallsink_nongl.py +++ b/gr-wxgui/src/python/waterfallsink_nongl.py @@ -26,7 +26,6 @@ import wx import gnuradio.wxgui.plot as plot import numpy import os -import threading import math default_fftsink_size = (640,240) @@ -148,37 +147,29 @@ class DataEvent(wx.PyEvent): def Clone (self): self.__class__ (self.GetId()) - - -class input_watcher (threading.Thread): + +class input_watcher (gru.msgq_runner): def __init__ (self, msgq, fft_size, event_receiver, **kwds): - threading.Thread.__init__ (self, **kwds) - self.setDaemon (1) - self.msgq = msgq self.fft_size = fft_size self.event_receiver = event_receiver - self.keep_running = True - self.start () - - def run (self): - while (self.keep_running): - msg = self.msgq.delete_head() # blocking read of message queue - itemsize = int(msg.arg1()) - nitems = int(msg.arg2()) - - s = msg.to_string() # get the body of the msg as a string - - # There may be more than one FFT frame in the message. - # If so, we take only the last one - if nitems > 1: - start = itemsize * (nitems - 1) - s = s[start:start+itemsize] - - complex_data = numpy.fromstring (s, numpy.float32) - de = DataEvent (complex_data) - wx.PostEvent (self.event_receiver, de) - del de - + gru.msgq_runner.__init__(self, msgq, self.handle_msg) + + def handle_msg(self, msg): + itemsize = int(msg.arg1()) + nitems = int(msg.arg2()) + + s = msg.to_string() # get the body of the msg as a string + + # There may be more than one FFT frame in the message. + # If so, we take only the last one + if nitems > 1: + start = itemsize * (nitems - 1) + s = s[start:start+itemsize] + + complex_data = numpy.fromstring (s, numpy.float32) + de = DataEvent (complex_data) + wx.PostEvent (self.event_receiver, de) + del de class waterfall_window (wx.Panel): def __init__ (self, fftsink, parent, id = -1,