eric6/DebugClients/Python/AsyncFile.py

changeset 8014
efa0c52fee56
parent 7926
5357e5ffebf1
child 8043
0acf98cd089a
child 8207
d359172d11be
equal deleted inserted replaced
8008:ae9ab1e150dc 8014:efa0c52fee56
7 Module implementing an asynchronous file like socket interface for the 7 Module implementing an asynchronous file like socket interface for the
8 debugger. 8 debugger.
9 """ 9 """
10 10
11 import socket 11 import socket
12 import threading
12 13
13 from DebugUtilities import prepareJsonCommand 14 from DebugUtilities import prepareJsonCommand
14 15
15 16
16 def AsyncPendingWrite(file): 17 def AsyncPendingWrite(file):
56 self.encoding = "utf-8" 57 self.encoding = "utf-8"
57 self.errors = None 58 self.errors = None
58 self.newlines = None 59 self.newlines = None
59 self.line_buffering = False 60 self.line_buffering = False
60 61
62 self.writeLock = threading.RLock()
61 self.wpending = [] 63 self.wpending = []
62 64
63 def __checkMode(self, mode): 65 def __checkMode(self, mode):
64 """ 66 """
65 Private method to check the mode. 67 Private method to check the mode.
97 99
98 def flush(self): 100 def flush(self):
99 """ 101 """
100 Public method to write all pending entries. 102 Public method to write all pending entries.
101 """ 103 """
104 self.writeLock.acquire()
102 while self.wpending: 105 while self.wpending:
103 try: 106 try:
104 buf = self.wpending.pop(0) 107 buf = self.wpending.pop(0)
105 except IndexError: 108 except IndexError:
106 break 109 break
114 self.nWriteErrors = 0 117 self.nWriteErrors = 0
115 except OSError: 118 except OSError:
116 self.nWriteErrors += 1 119 self.nWriteErrors += 1
117 if self.nWriteErrors > self.maxtries: 120 if self.nWriteErrors > self.maxtries:
118 self.wpending = [] # delete all output 121 self.wpending = [] # delete all output
122 self.writeLock.release()
119 123
120 def isatty(self): 124 def isatty(self):
121 """ 125 """
122 Public method to indicate whether a tty interface is supported. 126 Public method to indicate whether a tty interface is supported.
123 127
337 @param s text to be written 341 @param s text to be written
338 @type str, bytes or bytearray 342 @type str, bytes or bytearray
339 """ 343 """
340 self.__checkMode('w') 344 self.__checkMode('w')
341 345
346 self.writeLock.acquire()
342 if isinstance(s, (bytes, bytearray)): 347 if isinstance(s, (bytes, bytearray)):
343 # convert to string to send it 348 # convert to string to send it
344 s = repr(s) 349 s = repr(s)
345 350
346 cmd = prepareJsonCommand("ClientOutput", { 351 cmd = prepareJsonCommand("ClientOutput", {
347 "text": s, 352 "text": s,
348 "debuggerId": "", 353 "debuggerId": "",
349 }) 354 })
350 self.wpending.append(cmd) 355 self.wpending.append(cmd)
351 self.flush() 356 self.flush()
357 self.writeLock.release()
352 358
353 def write_p(self, s): 359 def write_p(self, s):
354 """ 360 """
355 Public method to write a json-rpc 2.0 coded string to the file. 361 Public method to write a json-rpc 2.0 coded string to the file.
356 362

eric ide

mercurial