##// END OF EJS Templates
Using threading.Lock in jroplotter.py
Miguel Valdez -
r707:17810a6cdcd3
parent child
Show More
@@ -1,111 +1,137
1 1 '''
2 2 Created on Jul 9, 2014
3 3
4 4 @author: roj-idl71
5 5 '''
6 6 import os
7 7 import datetime
8 8 import numpy
9 9
10 10 from time import sleep
11 11 from Queue import Queue
12 from threading import Thread
12 from threading import Lock
13 # from threading import Thread
13 14
14 15 from schainpy.model.proc.jroproc_base import Operation
15 16 from schainpy.model.serializer.data import obj2Dict, dict2Obj
16 17 from jroplot_correlation import *
17 18 from jroplot_heispectra import *
18 19 from jroplot_parameters import *
19 20 from jroplot_spectra import *
20 21 from jroplot_voltage import *
21 22
22 23
23 24 class Plotter(Operation):
24 25
25 26 isConfig = None
26 27 name = None
27 28 __queue = None
28 29
29 30 def __init__(self, plotter_name, plotter_queue=None):
30 31
31 32 Operation.__init__(self)
32 33
33 34 self.isConfig = False
34 35 self.name = plotter_name
35 36 self.__queue = plotter_queue
36 37
37 38 def getSubplots(self):
38 39
39 40 nrow = self.nplots
40 41 ncol = 1
41 42 return nrow, ncol
42 43
43 44 def setup(self, **kwargs):
44 45
45 46 print "Initializing ..."
46 47
47 48
48 49 def run(self, dataOut, id=None, **kwargs):
49 50
50 51 """
51 52
52 53 Input:
53 54 dataOut :
54 55 id :
55 56 """
56 57
57 58 packDict = {}
58 59
59 60 packDict['id'] = id
60 61 packDict['name'] = self.name
61 62 packDict['kwargs'] = kwargs
62 63
63 64 packDict['data'] = obj2Dict(dataOut)
64 65
65 66 self.__queue.put(packDict)
66 67
67 class PlotterManager(Thread):
68
68 # class PlotManager(Thread):
69 class PlotManager():
69 70 __stop = False
70 71
71 72 def __init__(self, plotter_queue):
72 73
73 Thread.__init__(self)
74
75 self.setDaemon(True)
74 # Thread.__init__(self)
75 # self.setDaemon(True)
76 76
77 77 self.__queue = plotter_queue
78 self.__lock = Lock()
78 79
79 80 self.plotInstanceDict = {}
80 81 self.__stop = False
81 82
82 83 def run(self):
83 84
84 while True:
85
86 if self.__stop:
87 break
85 if self.__queue.empty():
86 return
87
88 self.__lock.acquire()
89
90 # if self.__queue.full():
91 # for i in range(int(self.__queue.qsize()/2)):
92 # serial_data = self.__queue.get()
93 # self.__queue.task_done()
88 94
89 if self.__queue.empty():
90 sleep(0.1)
91 continue
95 n = int(self.__queue.qsize()/3 + 1)
96
97 for i in range(n):
92 98
93 serial_data = self.__queue.get(True)
99 serial_data = self.__queue.get()
100 self.__queue.task_done()
94 101
95 102 plot_id = serial_data['id']
96 103 plot_name = serial_data['name']
97 104 kwargs = serial_data['kwargs']
98 105 dataDict = serial_data['data']
99 106
100 107 dataPlot = dict2Obj(dataDict)
101 108
102 109 if plot_id not in self.plotInstanceDict.keys():
103 110 className = eval(plot_name)
104 111 self.plotInstanceDict[plot_id] = className()
105 112
106 113 plotter = self.plotInstanceDict[plot_id]
107 114 plotter.run(dataPlot, plot_id, **kwargs)
108 115
116
117
118 self.__lock.release()
119
109 120 def stop(self):
110 121
111 self.__stop = True No newline at end of file
122 self.__lock.acquire()
123
124 self.__stop = True
125
126 self.__lock.release()
127
128 def close(self):
129
130 self.__lock.acquire()
131
132 for plot_id in self.plotInstanceDict.keys():
133 plotter = self.plotInstanceDict[plot_id]
134 plotter.close()
135
136 self.__lock.release()
137 No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now