eric6/Utilities/BackgroundClient.py

branch
multi_processing
changeset 7646
39e3db2b4936
parent 7564
787684e6f2f3
parent 7639
422fd05e9c91
child 7802
eefe954f01e8
equal deleted inserted replaced
7627:812ee8c0a91a 7646:39e3db2b4936
7 """ 7 """
8 Module implementing a Qt free version of a background client for the various 8 Module implementing a Qt free version of a background client for the various
9 checkers and other python interpreter dependent functions. 9 checkers and other python interpreter dependent functions.
10 """ 10 """
11 11
12 from __future__ import unicode_literals 12 import io
13 try:
14 bytes = unicode # __IGNORE_EXCEPTION__
15 import StringIO as io # __IGNORE_EXCEPTION__
16 except NameError:
17 import io # __IGNORE_WARNING__
18
19 import json 13 import json
20 import socket 14 import socket
21 import struct 15 import struct
22 import sys 16 import sys
23 import time 17 import time
43 """ 37 """
44 self.services = {} 38 self.services = {}
45 self.batchServices = {} 39 self.batchServices = {}
46 40
47 self.connection = socket.create_connection((host, port)) 41 self.connection = socket.create_connection((host, port))
48 ver = b'Python2' if sys.version_info[0] == 2 else b'Python3' 42 ver = b'Python3'
49 self.connection.sendall(ver) 43 self.connection.sendall(ver)
50 self.__maxProcs = maxProcs 44 self.__maxProcs = maxProcs
51 45
52 def __initClientService(self, fn, path, module): 46 def __initClientService(self, fn, path, module):
53 """ 47 """
85 )): 79 )):
86 # handle sending of objects of unsupported types 80 # handle sending of objects of unsupported types
87 data = str(data) 81 data = str(data)
88 82
89 packedData = json.dumps([fx, fn, data]) 83 packedData = json.dumps([fx, fn, data])
90 if sys.version_info[0] >= 3: 84 packedData = bytes(packedData, 'utf-8')
91 packedData = bytes(packedData, 'utf-8')
92 header = struct.pack( 85 header = struct.pack(
93 b'!II', len(packedData), adler32(packedData) & 0xffffffff) 86 b'!II', len(packedData), adler32(packedData) & 0xffffffff)
94 self.connection.sendall(header) 87 self.connection.sendall(header)
95 self.connection.sendall(packedData) 88 self.connection.sendall(packedData)
96 89
142 return False 135 return False
143 136
144 def run(self): 137 def run(self):
145 """ 138 """
146 Public method implementing the main loop of the client. 139 Public method implementing the main loop of the client.
140
141 @exception RuntimeError raised if hashes don't match
147 """ 142 """
148 try: 143 try:
149 while True: 144 while True:
150 header = self.__receive(struct.calcsize(b'!II')) 145 header = self.__receive(struct.calcsize(b'!II'))
151 # Leave main loop if connection was closed. 146 # Leave main loop if connection was closed.
157 packedData = self.__receive(length) 152 packedData = self.__receive(length)
158 153
159 if messageType != b"JOB ": 154 if messageType != b"JOB ":
160 continue 155 continue
161 156
162 assert adler32(packedData) & 0xffffffff == datahash, \ 157 if adler32(packedData) & 0xffffffff != datahash:
163 'Hashes not equal' 158 raise RuntimeError('Hashes not equal')
164 if sys.version_info[0] >= 3: 159
165 packedData = packedData.decode('utf-8') 160 packedData = packedData.decode('utf-8')
166 161
167 fx, fn, data = json.loads(packedData) 162 fx, fn, data = json.loads(packedData)
168 if fx == 'INIT': 163 if fx == 'INIT':
169 ret = self.__initClientService(fn, *data) 164 ret = self.__initClientService(fn, *data)
170 elif fx.startswith("batch_"): 165 elif fx.startswith("batch_"):

eric ide

mercurial