Utilities/BackgroundService.py

branch
BgService
changeset 3209
c5432abceb25
parent 3173
1fb284abe46e
child 3241
957673fc463a
equal deleted inserted replaced
3177:5af61402d74d 3209:c5432abceb25
38 """ 38 """
39 Constructor of the BackgroundService class. 39 Constructor of the BackgroundService class.
40 """ 40 """
41 self.processes = [None, None] 41 self.processes = [None, None]
42 self.connections = [None, None] 42 self.connections = [None, None]
43 self.isWorking = False 43 self.isWorking = None
44 self.__queue = [] 44 self.__queue = []
45 self.services = {} 45 self.services = {}
46 46
47 super(BackgroundService, self).__init__() 47 super(BackgroundService, self).__init__()
48 48
109 def __processQueue(self): 109 def __processQueue(self):
110 """ 110 """
111 Private method to take the next service request and send it to the 111 Private method to take the next service request and send it to the
112 client. 112 client.
113 """ 113 """
114 if self.__queue and self.isWorking is False: 114 if self.__queue and self.isWorking is None:
115 self.isWorking = True
116 fx, fn, pyVer, data = self.__queue.pop(0) 115 fx, fn, pyVer, data = self.__queue.pop(0)
116 self.isWorking = pyVer
117 self.__send(fx, fn, pyVer, data) 117 self.__send(fx, fn, pyVer, data)
118 118
119 def __send(self, fx, fn, pyVer, data): 119 def __send(self, fx, fn, pyVer, data):
120 """ 120 """
121 Private method to send a job request to one of the clients. 121 Private method to send a job request to one of the clients.
133 if fx != 'INIT': 133 if fx != 'INIT':
134 self.serviceNotAvailable.emit( 134 self.serviceNotAvailable.emit(
135 fx, fn, pyVer, self.trUtf8( 135 fx, fn, pyVer, self.trUtf8(
136 'Python{0} interpreter not configured.').format(pyVer)) 136 'Python{0} interpreter not configured.').format(pyVer))
137 # Reset flag and continue processing queue 137 # Reset flag and continue processing queue
138 self.isWorking = False 138 self.isWorking = None
139 self.__processQueue() 139 self.__processQueue()
140 else: 140 else:
141 header = struct.pack( 141 header = struct.pack(
142 b'!II', len(packedData), adler32(packedData) & 0xffffffff) 142 b'!II', len(packedData), adler32(packedData) & 0xffffffff)
143 connection.write(header) 143 connection.write(header)
184 else: 184 else:
185 callback = self.services.get(fx) 185 callback = self.services.get(fx)
186 if callback: 186 if callback:
187 callback[2](fn, *data) 187 callback[2](fn, *data)
188 188
189 self.isWorking = False 189 self.isWorking = None
190 self.__processQueue() 190 self.__processQueue()
191 191
192 def enqueueRequest(self, fx, fn, pyVer, data): 192 def enqueueRequest(self, fx, fn, pyVer, data):
193 """ 193 """
194 Implement a queued processing of incomming events. 194 Implement a queued processing of incomming events.
243 """ 243 """
244 connection = self.nextPendingConnection() 244 connection = self.nextPendingConnection()
245 if not connection.waitForReadyRead(1000): 245 if not connection.waitForReadyRead(1000):
246 return 246 return
247 ch = 0 if connection.read(1) == b'2' else 1 247 ch = 0 if connection.read(1) == b'2' else 1
248 # Avoid hanging of eric on shutdown
249 if self.connections[ch]:
250 self.connections[ch].close()
251 if self.isWorking == ch + 2:
252 self.isWorking = None
248 self.connections[ch] = connection 253 self.connections[ch] = connection
249 connection.readyRead.connect( 254 connection.readyRead.connect(
250 lambda x=ch: self.__receive(x)) 255 lambda x=ch: self.__receive(x))
251 256
252 for fx, args in self.services.items(): 257 for fx, args in self.services.items():

eric ide

mercurial