eric6/Utilities/BackgroundClient.py

Sat, 27 Feb 2021 12:08:23 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 27 Feb 2021 12:08:23 +0100
changeset 8138
169e65a6787c
parent 7965
48fbf9149d16
child 8043
0acf98cd089a
child 8207
d359172d11be
permissions
-rw-r--r--

Shell: added functionality to show a prompt when the main client process has exited (e.g. a script ended).

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
7923
91e843545d9a Updated copyright for 2021.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7853
diff changeset
3 # Copyright (c) 2013 - 2021 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 """
7965
48fbf9149d16 BackgroundService and -Client: fixed a few typos.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
28 Constructor
3159
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
7965
48fbf9149d16 BackgroundService and -Client: fixed a few typos.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
50 @param fn service name to register
48fbf9149d16 BackgroundService and -Client: fixed a few typos.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
51 @type str
48fbf9149d16 BackgroundService and -Client: fixed a few typos.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
52 @param path contains the path to the module
48fbf9149d16 BackgroundService and -Client: fixed a few typos.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
53 @type str
48fbf9149d16 BackgroundService and -Client: fixed a few typos.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
54 @param module name to import
48fbf9149d16 BackgroundService and -Client: fixed a few typos.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
55 @type str
48fbf9149d16 BackgroundService and -Client: fixed a few typos.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
56 @return text result of the import action
48fbf9149d16 BackgroundService and -Client: fixed a few typos.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
57 @rtype str
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
58 """
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
59 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
60 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
61 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
62 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
63 try:
7259
7c017076c12e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
64 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
65 importedModule.initBatchService()
7259
7c017076c12e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
66 )
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
67 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
68 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
69 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
70 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
71 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
72
3159
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
73 def __send(self, fx, fn, data):
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
74 """
7965
48fbf9149d16 BackgroundService and -Client: fixed a few typos.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
75 Private method to send a job response back to the BackgroundService
48fbf9149d16 BackgroundService and -Client: fixed a few typos.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
76 server.
3159
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
77
7965
48fbf9149d16 BackgroundService and -Client: fixed a few typos.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
78 @param fx remote function name to execute
48fbf9149d16 BackgroundService and -Client: fixed a few typos.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
79 @type str
48fbf9149d16 BackgroundService and -Client: fixed a few typos.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
80 @param fn filename for identification
48fbf9149d16 BackgroundService and -Client: fixed a few typos.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
81 @type str
48fbf9149d16 BackgroundService and -Client: fixed a few typos.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
82 @param data return value(s)
48fbf9149d16 BackgroundService and -Client: fixed a few typos.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
83 @type any basic datatype
3159
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
84 """
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
85 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
86 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
87 )):
5441fa55cb0d BackgroundClient: implemented a solution to send data of types not supported by JSON.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
88 # 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
89 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
90
3159
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
91 packedData = json.dumps([fx, fn, data])
7637
c878e8255972 Removed some more Python2 related code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7635
diff changeset
92 packedData = bytes(packedData, 'utf-8')
3159
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
93 header = struct.pack(
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
94 b'!II', len(packedData), adler32(packedData) & 0xffffffff)
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
95 self.connection.sendall(header)
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
96 self.connection.sendall(packedData)
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
97
3444
118cb489304d Bugfix to avoid hanging Eric5 on shutdown.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3417
diff changeset
98 def __receive(self, length):
118cb489304d Bugfix to avoid hanging Eric5 on shutdown.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3417
diff changeset
99 """
7965
48fbf9149d16 BackgroundService and -Client: fixed a few typos.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
100 Private method to receive the given length of bytes.
3444
118cb489304d Bugfix to avoid hanging Eric5 on shutdown.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3417
diff changeset
101
7965
48fbf9149d16 BackgroundService and -Client: fixed a few typos.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
102 @param length bytes to receive
48fbf9149d16 BackgroundService and -Client: fixed a few typos.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
103 @type int
48fbf9149d16 BackgroundService and -Client: fixed a few typos.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
104 @return received bytes or None if connection closed
48fbf9149d16 BackgroundService and -Client: fixed a few typos.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
105 @rtype bytes
3444
118cb489304d Bugfix to avoid hanging Eric5 on shutdown.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3417
diff changeset
106 """
118cb489304d Bugfix to avoid hanging Eric5 on shutdown.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3417
diff changeset
107 data = b''
118cb489304d Bugfix to avoid hanging Eric5 on shutdown.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3417
diff changeset
108 while len(data) < length:
118cb489304d Bugfix to avoid hanging Eric5 on shutdown.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3417
diff changeset
109 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
110 if not newData:
118cb489304d Bugfix to avoid hanging Eric5 on shutdown.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3417
diff changeset
111 return None
118cb489304d Bugfix to avoid hanging Eric5 on shutdown.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3417
diff changeset
112 data += newData
118cb489304d Bugfix to avoid hanging Eric5 on shutdown.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3417
diff changeset
113 return data
118cb489304d Bugfix to avoid hanging Eric5 on shutdown.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3417
diff changeset
114
4221
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
115 def __peek(self, length):
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
116 """
7965
48fbf9149d16 BackgroundService and -Client: fixed a few typos.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
117 Private method to peek the given length of bytes.
4221
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
118
7965
48fbf9149d16 BackgroundService and -Client: fixed a few typos.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
119 @param length bytes to receive
48fbf9149d16 BackgroundService and -Client: fixed a few typos.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
120 @type int
48fbf9149d16 BackgroundService and -Client: fixed a few typos.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
121 @return received bytes
48fbf9149d16 BackgroundService and -Client: fixed a few typos.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
122 @rtype bytes
4221
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
123 """
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
124 data = b''
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
125 self.connection.setblocking(False)
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
126 try:
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
127 data = self.connection.recv(length, socket.MSG_PEEK)
7836
2f0d208b8137 Changed code to not use the OSError aliases (IOError, EnvironmentError, socket.error and select.error) anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7660
diff changeset
128 except OSError:
4221
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
129 pass
6302
8bef84b53fbe Don't post socket.error exceptions to eric.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
130 finally:
8bef84b53fbe Don't post socket.error exceptions to eric.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
131 self.connection.setblocking(True)
8bef84b53fbe Don't post socket.error exceptions to eric.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
132
4221
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
133 return data
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
134
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
135 def __cancelled(self):
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
136 """
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
137 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
138
7965
48fbf9149d16 BackgroundService and -Client: fixed a few typos.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
139 @return flag indicating a cancellation
48fbf9149d16 BackgroundService and -Client: fixed a few typos.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
140 @rtype bool
4221
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
141 """
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
142 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
143 if msg[-6:] == b"CANCEL":
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
144 # 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
145 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
146 return True
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
147 else:
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
148 return False
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
149
3159
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
150 def run(self):
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
151 """
3591
2f2a4a76dd22 Corrected a bunch of source docu issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3540
diff changeset
152 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
153
f904d0eef264 Checked the reported security related issue reports generated by the new security checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7534
diff changeset
154 @exception RuntimeError raised if hashes don't match
3159
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
155 """
3417
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
156 try:
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
157 while True:
4221
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
158 header = self.__receive(struct.calcsize(b'!II'))
3159
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
159 # 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
160 if not header:
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
161 break
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
162
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
163 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
164 messageType = self.__receive(6)
3444
118cb489304d Bugfix to avoid hanging Eric5 on shutdown.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3417
diff changeset
165 packedData = self.__receive(length)
3417
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
166
4221
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
167 if messageType != b"JOB ":
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
168 continue
c9fdc07753a7 Implemented the Cancel logic for batch checks.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4218
diff changeset
169
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
170 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
171 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
172
7637
c878e8255972 Removed some more Python2 related code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7635
diff changeset
173 packedData = packedData.decode('utf-8')
3417
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
174
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
175 fx, fn, data = json.loads(packedData)
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
176 if fx == 'INIT':
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
177 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
178 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
179 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
180 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
181 try:
76ef5f340007 Added functionality to limit the number of processes used for bachground services.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5624
diff changeset
182 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
183 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
184 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
185 # 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
186 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
187 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
188 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
189 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
190 else:
3417
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
191 callback = self.services.get(fx)
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
192 if callback:
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
193 ret = callback(fn, *data)
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
194 else:
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
195 ret = 'Unknown service.'
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
196
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
197 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
198 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
199
3417
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
200 self.__send(fx, fn, ret)
7836
2f0d208b8137 Changed code to not use the OSError aliases (IOError, EnvironmentError, socket.error and select.error) anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7660
diff changeset
201 except OSError:
6302
8bef84b53fbe Don't post socket.error exceptions to eric.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
202 pass
4563
881340f4bd0c Fixed a few coding style issue related to 'blind except:' statements.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4221
diff changeset
203 except Exception:
3417
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
204 exctype, excval, exctb = sys.exc_info()
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
205 tbinfofile = io.StringIO()
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
206 traceback.print_tb(exctb, None, tbinfofile)
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
207 tbinfofile.seek(0)
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
208 tbinfo = tbinfofile.read()
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
209 del exctb
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
210 self.__send(
5a93c6cdc989 Exception handling for client side implemented.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3241
diff changeset
211 '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
212
8bef84b53fbe Don't post socket.error exceptions to eric.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
213 finally:
8bef84b53fbe Don't post socket.error exceptions to eric.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
214 # 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
215 time.sleep(0.5)
8bef84b53fbe Don't post socket.error exceptions to eric.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
216 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
217 self.connection.close()
3159
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
218
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
219 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
220 if len(sys.argv) != 4:
7965
48fbf9149d16 BackgroundService and -Client: fixed a few typos.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
221 print('Host, port and max. processes parameters are missing.'
48fbf9149d16 BackgroundService and -Client: fixed a few typos.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
222 ' Aborting.')
3159
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
223 sys.exit(1)
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
224
5762
76ef5f340007 Added functionality to limit the number of processes used for bachground services.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5624
diff changeset
225 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
226 backgroundClient = BackgroundClient(host, int(port), int(maxProcs))
3159
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
227 # Start the main loop
02cb2adb4868 First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
228 backgroundClient.run()
4566
a2e8f3c420ec Dealt with the M801 code style checker messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4563
diff changeset
229
a2e8f3c420ec Dealt with the M801 code style checker messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4563
diff changeset
230 #
a2e8f3c420ec Dealt with the M801 code style checker messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4563
diff changeset
231 # eflag: noqa = M801

eric ide

mercurial