eric6/Utilities/BackgroundClient.py

Mon, 12 Oct 2020 19:28:42 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Mon, 12 Oct 2020 19:28:42 +0200
changeset 7781
607a6098cb44
parent 7660
87291331c122
child 7802
eefe954f01e8
child 7836
2f0d208b8137
permissions
-rw-r--r--

Performed some more code cleanup.

3159
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
2
7360
9190402e4505 Updated copyright for 2020.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7259
diff changeset
3 # Copyright (c) 2013 - 2020 Detlev Offenbach <detlev@die-offenbachs.de>
3159
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
4 #
3173
1fb284abe46e Interface to add user-defined services, e.g. in plugins. Auto syntax check working. Little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
5 # pylint: disable=C0103
3159
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
6
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
7 """
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
8 Module implementing a Qt free version of a background client for the various
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
9 checkers and other python interpreter dependent functions.
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
10 """
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
11
7639
422fd05e9c91 Removed some more Python2 related code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7637
diff changeset
12 import io
3159
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
13 import json
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
14 import socket
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
15 import struct
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
16 import sys
3744
cc879190cef7 Exception on exception handling of the background service on slow computers fixed.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3591
diff changeset
17 import time
3417
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
18 import traceback
3159
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
19 from zlib import adler32
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
20
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
21
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
22 class BackgroundClient(object):
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
23 """
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
24 Class implementing the main part of the background client.
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
25 """
5762
76ef5f340007 Added functionality to limit the number of processes used for bachground services.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5624
diff changeset
26 def __init__(self, host, port, maxProcs):
3159
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
27 """
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
28 Constructor of the BackgroundClient class.
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
29
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
30 @param host ip address the background service is listening
5762
76ef5f340007 Added functionality to limit the number of processes used for bachground services.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5624
diff changeset
31 @type str
3159
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
32 @param port port of the background service
5762
76ef5f340007 Added functionality to limit the number of processes used for bachground services.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5624
diff changeset
33 @type int
76ef5f340007 Added functionality to limit the number of processes used for bachground services.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5624
diff changeset
34 @param maxProcs maximum number of CPUs (processes) to use
76ef5f340007 Added functionality to limit the number of processes used for bachground services.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5624
diff changeset
35 (0 = determined automatically)
76ef5f340007 Added functionality to limit the number of processes used for bachground services.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5624
diff changeset
36 @type int
3159
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
37 """
3173
1fb284abe46e Interface to add user-defined services, e.g. in plugins. Auto syntax check working. Little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
38 self.services = {}
4218
f542ad1f76c5 Added a batch mode to the code style checker to make use of multiple CPUs/CPU-Cores.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4021
diff changeset
39 self.batchServices = {}
3173
1fb284abe46e Interface to add user-defined services, e.g. in plugins. Auto syntax check working. Little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
40
3159
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
41 self.connection = socket.create_connection((host, port))
7635
0cdead130a81 Removed support for Python2 and removed support for Qt4 (PyQt4 and pyside).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7628
diff changeset
42 ver = b'Python3'
3159
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
43 self.connection.sendall(ver)
5762
76ef5f340007 Added functionality to limit the number of processes used for bachground services.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5624
diff changeset
44 self.__maxProcs = maxProcs
3159
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
45
3173
1fb284abe46e Interface to add user-defined services, e.g. in plugins. Auto syntax check working. Little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
46 def __initClientService(self, fn, path, module):
1fb284abe46e Interface to add user-defined services, e.g. in plugins. Auto syntax check working. Little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
47 """
3591
2f2a4a76dd22 Corrected a bunch of source docu issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3540
diff changeset
48 Private method to import the given module and register it as service.
3173
1fb284abe46e Interface to add user-defined services, e.g. in plugins. Auto syntax check working. Little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
49
1fb284abe46e Interface to add user-defined services, e.g. in plugins. Auto syntax check working. Little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
50 @param fn service name to register (str)
1fb284abe46e Interface to add user-defined services, e.g. in plugins. Auto syntax check working. Little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
51 @param path contains the path to the module (str)
1fb284abe46e Interface to add user-defined services, e.g. in plugins. Auto syntax check working. Little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
52 @param module name to import (str)
1fb284abe46e Interface to add user-defined services, e.g. in plugins. Auto syntax check working. Little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
53 @return text result of the import action (str)
1fb284abe46e Interface to add user-defined services, e.g. in plugins. Auto syntax check working. Little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
54 """
3540
b83fe0651bfd Fixed issue in background service if module is already installed in sys.path.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3444
diff changeset
55 sys.path.insert(1, path)
3173
1fb284abe46e Interface to add user-defined services, e.g. in plugins. Auto syntax check working. Little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
56 try:
1fb284abe46e Interface to add user-defined services, e.g. in plugins. Auto syntax check working. Little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
57 importedModule = __import__(module, globals(), locals(), [], 0)
1fb284abe46e Interface to add user-defined services, e.g. in plugins. Auto syntax check working. Little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
58 self.services[fn] = importedModule.initService()
4218
f542ad1f76c5 Added a batch mode to the code style checker to make use of multiple CPUs/CPU-Cores.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4021
diff changeset
59 try:
7259
7c017076c12e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
60 self.batchServices["batch_" + fn] = (
4218
f542ad1f76c5 Added a batch mode to the code style checker to make use of multiple CPUs/CPU-Cores.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4021
diff changeset
61 importedModule.initBatchService()
7259
7c017076c12e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
62 )
4218
f542ad1f76c5 Added a batch mode to the code style checker to make use of multiple CPUs/CPU-Cores.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4021
diff changeset
63 except AttributeError:
f542ad1f76c5 Added a batch mode to the code style checker to make use of multiple CPUs/CPU-Cores.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4021
diff changeset
64 pass
3173
1fb284abe46e Interface to add user-defined services, e.g. in plugins. Auto syntax check working. Little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
65 return 'ok'
1fb284abe46e Interface to add user-defined services, e.g. in plugins. Auto syntax check working. Little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
66 except ImportError:
1fb284abe46e Interface to add user-defined services, e.g. in plugins. Auto syntax check working. Little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
67 return 'Import Error'
1fb284abe46e Interface to add user-defined services, e.g. in plugins. Auto syntax check working. Little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
68
3159
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
69 def __send(self, fx, fn, data):
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
70 """
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
71 Private method to send a job response back to the BackgroundService.
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
72
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
73 @param fx remote function name to execute (str)
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
74 @param fn filename for identification (str)
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
75 @param data return value(s) (any basic datatype)
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
76 """
7534
5441fa55cb0d BackgroundClient: implemented a solution to send data of types not supported by JSON.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
77 if not isinstance(data, (
5441fa55cb0d BackgroundClient: implemented a solution to send data of types not supported by JSON.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
78 dict, list, tuple, str, int, float, bool, type(None),
5441fa55cb0d BackgroundClient: implemented a solution to send data of types not supported by JSON.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
79 )):
5441fa55cb0d BackgroundClient: implemented a solution to send data of types not supported by JSON.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
80 # handle sending of objects of unsupported types
5441fa55cb0d BackgroundClient: implemented a solution to send data of types not supported by JSON.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
81 data = str(data)
5441fa55cb0d BackgroundClient: implemented a solution to send data of types not supported by JSON.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
82
3159
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
83 packedData = json.dumps([fx, fn, data])
7637
c878e8255972 Removed some more Python2 related code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7635
diff changeset
84 packedData = bytes(packedData, 'utf-8')
3159
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
85 header = struct.pack(
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
86 b'!II', len(packedData), adler32(packedData) & 0xffffffff)
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
87 self.connection.sendall(header)
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
88 self.connection.sendall(packedData)
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
89
3444
118cb489304d Bugfix to avoid hanging Eric5 on shutdown.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3417
diff changeset
90 def __receive(self, length):
118cb489304d Bugfix to avoid hanging Eric5 on shutdown.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3417
diff changeset
91 """
118cb489304d Bugfix to avoid hanging Eric5 on shutdown.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3417
diff changeset
92 Private methode to receive the given length of bytes.
118cb489304d Bugfix to avoid hanging Eric5 on shutdown.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3417
diff changeset
93
118cb489304d Bugfix to avoid hanging Eric5 on shutdown.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3417
diff changeset
94 @param length bytes to receive (int)
4221
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
95 @return received bytes or None if connection closed (bytes)
3444
118cb489304d Bugfix to avoid hanging Eric5 on shutdown.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3417
diff changeset
96 """
118cb489304d Bugfix to avoid hanging Eric5 on shutdown.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3417
diff changeset
97 data = b''
118cb489304d Bugfix to avoid hanging Eric5 on shutdown.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3417
diff changeset
98 while len(data) < length:
118cb489304d Bugfix to avoid hanging Eric5 on shutdown.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3417
diff changeset
99 newData = self.connection.recv(length - len(data))
118cb489304d Bugfix to avoid hanging Eric5 on shutdown.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3417
diff changeset
100 if not newData:
118cb489304d Bugfix to avoid hanging Eric5 on shutdown.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3417
diff changeset
101 return None
118cb489304d Bugfix to avoid hanging Eric5 on shutdown.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3417
diff changeset
102 data += newData
118cb489304d Bugfix to avoid hanging Eric5 on shutdown.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3417
diff changeset
103 return data
118cb489304d Bugfix to avoid hanging Eric5 on shutdown.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3417
diff changeset
104
4221
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
105 def __peek(self, length):
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
106 """
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
107 Private methode to peek the given length of bytes.
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
108
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
109 @param length bytes to receive (int)
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
110 @return received bytes (bytes)
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
111 """
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
112 data = b''
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
113 self.connection.setblocking(False)
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
114 try:
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
115 data = self.connection.recv(length, socket.MSG_PEEK)
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
116 except socket.error:
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
117 pass
6302
8bef84b53fbe Don't post socket.error exceptions to eric.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
118 finally:
8bef84b53fbe Don't post socket.error exceptions to eric.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
119 self.connection.setblocking(True)
8bef84b53fbe Don't post socket.error exceptions to eric.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
120
4221
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
121 return data
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
122
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
123 def __cancelled(self):
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
124 """
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
125 Private method to check for a job cancellation.
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
126
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
127 @return flag indicating a cancellation (boolean)
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
128 """
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
129 msg = self.__peek(struct.calcsize(b'!II') + 6)
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
130 if msg[-6:] == b"CANCEL":
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
131 # get rid of the message data
6302
8bef84b53fbe Don't post socket.error exceptions to eric.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
132 self.__receive(struct.calcsize(b'!II') + 6)
4221
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
133 return True
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
134 else:
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
135 return False
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
136
3159
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
137 def run(self):
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
138 """
3591
2f2a4a76dd22 Corrected a bunch of source docu issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3540
diff changeset
139 Public method implementing the main loop of the client.
7628
f904d0eef264 Checked the reported security related issue reports generated by the new security checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7534
diff changeset
140
f904d0eef264 Checked the reported security related issue reports generated by the new security checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7534
diff changeset
141 @exception RuntimeError raised if hashes don't match
3159
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
142 """
3417
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
143 try:
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
144 while True:
4221
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
145 header = self.__receive(struct.calcsize(b'!II'))
3159
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
146 # Leave main loop if connection was closed.
3417
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
147 if not header:
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
148 break
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
149
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
150 length, datahash = struct.unpack(b'!II', header)
4221
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
151 messageType = self.__receive(6)
3444
118cb489304d Bugfix to avoid hanging Eric5 on shutdown.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3417
diff changeset
152 packedData = self.__receive(length)
3417
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
153
4221
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
154 if messageType != b"JOB ":
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
155 continue
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
156
7628
f904d0eef264 Checked the reported security related issue reports generated by the new security checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7534
diff changeset
157 if adler32(packedData) & 0xffffffff != datahash:
f904d0eef264 Checked the reported security related issue reports generated by the new security checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7534
diff changeset
158 raise RuntimeError('Hashes not equal')
f904d0eef264 Checked the reported security related issue reports generated by the new security checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7534
diff changeset
159
7637
c878e8255972 Removed some more Python2 related code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7635
diff changeset
160 packedData = packedData.decode('utf-8')
3417
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
161
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
162 fx, fn, data = json.loads(packedData)
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
163 if fx == 'INIT':
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
164 ret = self.__initClientService(fn, *data)
4218
f542ad1f76c5 Added a batch mode to the code style checker to make use of multiple CPUs/CPU-Cores.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4021
diff changeset
165 elif fx.startswith("batch_"):
f542ad1f76c5 Added a batch mode to the code style checker to make use of multiple CPUs/CPU-Cores.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4021
diff changeset
166 callback = self.batchServices.get(fx)
f542ad1f76c5 Added a batch mode to the code style checker to make use of multiple CPUs/CPU-Cores.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4021
diff changeset
167 if callback:
5762
76ef5f340007 Added functionality to limit the number of processes used for bachground services.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5624
diff changeset
168 try:
76ef5f340007 Added functionality to limit the number of processes used for bachground services.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5624
diff changeset
169 callback(data, self.__send, fx, self.__cancelled,
76ef5f340007 Added functionality to limit the number of processes used for bachground services.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5624
diff changeset
170 maxProcesses=self.__maxProcs)
76ef5f340007 Added functionality to limit the number of processes used for bachground services.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5624
diff changeset
171 except TypeError:
76ef5f340007 Added functionality to limit the number of processes used for bachground services.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5624
diff changeset
172 # for backward compatibility
76ef5f340007 Added functionality to limit the number of processes used for bachground services.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5624
diff changeset
173 callback(data, self.__send, fx, self.__cancelled)
4218
f542ad1f76c5 Added a batch mode to the code style checker to make use of multiple CPUs/CPU-Cores.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4021
diff changeset
174 ret = "__DONE__"
f542ad1f76c5 Added a batch mode to the code style checker to make use of multiple CPUs/CPU-Cores.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4021
diff changeset
175 else:
f542ad1f76c5 Added a batch mode to the code style checker to make use of multiple CPUs/CPU-Cores.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4021
diff changeset
176 ret = 'Unknown batch service.'
3173
1fb284abe46e Interface to add user-defined services, e.g. in plugins. Auto syntax check working. Little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3159
diff changeset
177 else:
3417
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
178 callback = self.services.get(fx)
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
179 if callback:
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
180 ret = callback(fn, *data)
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
181 else:
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
182 ret = 'Unknown service.'
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
183
7660
87291331c122 BackgroundClient: added code to convert returned exceptions to string before sending them as json.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
184 if isinstance(ret, Exception):
87291331c122 BackgroundClient: added code to convert returned exceptions to string before sending them as json.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
185 ret = str(ret)
87291331c122 BackgroundClient: added code to convert returned exceptions to string before sending them as json.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
186
3417
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
187 self.__send(fx, fn, ret)
6302
8bef84b53fbe Don't post socket.error exceptions to eric.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
188 except socket.error:
8bef84b53fbe Don't post socket.error exceptions to eric.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
189 pass
4563
881340f4bd0c Fixed a few coding style issue related to 'blind except:' statements.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4221
diff changeset
190 except Exception:
3417
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
191 exctype, excval, exctb = sys.exc_info()
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
192 tbinfofile = io.StringIO()
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
193 traceback.print_tb(exctb, None, tbinfofile)
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
194 tbinfofile.seek(0)
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
195 tbinfo = tbinfofile.read()
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
196 del exctb
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
197 self.__send(
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
198 'EXCEPTION', '?', [str(exctype), str(excval), tbinfo])
6302
8bef84b53fbe Don't post socket.error exceptions to eric.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
199
8bef84b53fbe Don't post socket.error exceptions to eric.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
200 finally:
8bef84b53fbe Don't post socket.error exceptions to eric.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
201 # Give time to process latest response on server side
8bef84b53fbe Don't post socket.error exceptions to eric.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
202 time.sleep(0.5)
8bef84b53fbe Don't post socket.error exceptions to eric.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
203 self.connection.shutdown(socket.SHUT_RDWR)
8bef84b53fbe Don't post socket.error exceptions to eric.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
204 self.connection.close()
3159
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
205
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
206 if __name__ == '__main__':
5762
76ef5f340007 Added functionality to limit the number of processes used for bachground services.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5624
diff changeset
207 if len(sys.argv) != 4:
76ef5f340007 Added functionality to limit the number of processes used for bachground services.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5624
diff changeset
208 print('Host, port and max. processes parameters are missing. Abort.')
3159
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
209 sys.exit(1)
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
210
5762
76ef5f340007 Added functionality to limit the number of processes used for bachground services.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5624
diff changeset
211 host, port, maxProcs = sys.argv[1:]
76ef5f340007 Added functionality to limit the number of processes used for bachground services.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5624
diff changeset
212 backgroundClient = BackgroundClient(host, int(port), int(maxProcs))
3159
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
213 # Start the main loop
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
214 backgroundClient.run()
4566
a2e8f3c420ec Dealt with the M801 code style checker messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4563
diff changeset
215
a2e8f3c420ec Dealt with the M801 code style checker messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4563
diff changeset
216 #
a2e8f3c420ec Dealt with the M801 code style checker messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4563
diff changeset
217 # eflag: noqa = M801

eric ide

mercurial