Skip to content

Commit 009dd15

Browse files
author
Zhengbo Li
committed
Add worker thread
1 parent 7233d1a commit 009dd15

3 files changed

Lines changed: 48 additions & 4 deletions

File tree

typescript/libs/global_vars.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
# set logging levels
3939
LOG_FILE_LEVEL = logging.WARN
40-
LOG_CONSOLE_LEVEL = logging.WARN
40+
LOG_CONSOLE_LEVEL = logging.DEBUG
4141

4242
NON_BLANK_LINE_PATTERN = re.compile("[\S]+")
4343
VALID_COMPLETION_ID_PATTERN = re.compile("[a-zA-Z_$\.][\w$\.]*\Z")

typescript/libs/node_client.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ def sendCmdSync(self, cmd): pass
2727

2828
def sendCmdAsync(self, cmd): pass
2929

30+
def postCmdToWorker(self, cmd): pass
31+
3032

3133
class NodeCommClient(CommClient):
3234
__CONTENT_LENGTH_HEADER = b"Content-Length: "
@@ -59,6 +61,7 @@ def __init__(self, scriptPath):
5961
print("Unable to find executable file for node on path list: " + path_list)
6062
print("To specify the node executable file name, use the 'node_path' setting")
6163
self.__serverProc = None
64+
self.__workerProc = None
6265
else:
6366
print("Found node executable at " + node_path)
6467
try:
@@ -69,12 +72,17 @@ def __init__(self, scriptPath):
6972
si.dwFlags |= subprocess.SW_HIDE | subprocess.STARTF_USESHOWWINDOW
7073
self.__serverProc = subprocess.Popen([node_path, scriptPath],
7174
stdin=subprocess.PIPE, stdout=subprocess.PIPE, startupinfo=si)
75+
self.__workerProc = subprocess.Popen([node_path, scriptPath],
76+
stdin=subprocess.PIPE, stdout=subprocess.PIPE, startupinfo=si)
7277
else:
7378
log.debug("opening " + node_path + " " + scriptPath)
7479
self.__serverProc = subprocess.Popen([node_path, scriptPath],
7580
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
81+
self.__workerProc = subprocess.Popen([node_path, scriptPath],
82+
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
7683
except:
7784
self.__serverProc = None
85+
self.__workerProc = None
7886
# start reader thread
7987
if self.__serverProc and (not self.__serverProc.poll()):
8088
log.debug("server proc " + str(self.__serverProc))
@@ -83,12 +91,29 @@ def __init__(self, scriptPath):
8391
self.__serverProc.stdout, self.__msgq, self.__eventq, self.asyncReq, self.__serverProc))
8492
readerThread.daemon = True
8593
readerThread.start()
94+
95+
# start the worker thread
96+
if self.__workerProc and not self.__workerProc.poll():
97+
log.debug("worker proc {0}".format(self.__workerProc))
98+
log.debug("starting worker thread")
99+
worker_thread = threading.Thread(
100+
target = NodeCommClient.__reader,
101+
args = (
102+
self.__workerProc.stdout, self.__msgq, self.__eventq, self.asyncReq, self.__workerProc
103+
)
104+
)
105+
worker_thread.daemon = True
106+
worker_thread.start()
107+
86108
self.__debugProc = None
87109
self.__breakpoints = []
88110

89111
def serverStarted(self):
90112
return self.__serverProc is not None
91113

114+
def workerStarted(self):
115+
return self.__workerProc is not None
116+
92117
# work in progress
93118
def addBreakpoint(self, file, line):
94119
self.__breakpoints.append((file, line))
@@ -172,6 +197,20 @@ def postCmd(self, cmd):
172197
self.__serverProc.stdin.flush()
173198
return True
174199

200+
def postCmdToWorker(self, cmd):
201+
"""
202+
Post command to worker process; no response needed
203+
"""
204+
log.debug('Posting command to worker: {0}'.format(cmd))
205+
if not self.__workerProc:
206+
log.error("can not send request; worker process not running")
207+
return False
208+
else:
209+
cmd += "\n"
210+
self.__workerProc.stdin.write(cmd.encode())
211+
self.__workerProc.stdin.flush()
212+
return True
213+
175214
def getEvent(self):
176215
"""
177216
Try to get event from event queue

typescript/libs/service_proxy.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def exit(self):
1919
req_dict = self.create_req_dict("exit")
2020
json_str = json_helpers.encode(req_dict)
2121
self.__comm.postCmd(json_str)
22+
self.__comm.postCmdToWorker(json_str)
2223

2324
def configure(self, host_info="Sublime Text", file=None, format_options=None):
2425
args = {"hostInfo": host_info, "formatOptions": format_options, "file": file}
@@ -39,6 +40,7 @@ def change(self, path, begin_location=Location(1, 1), end_location=Location(1, 1
3940
req_dict = self.create_req_dict("change", args)
4041
json_str = json_helpers.encode(req_dict)
4142
self.__comm.postCmd(json_str)
43+
self.__comm.postCmdToWorker(json_str)
4244

4345
def completions(self, path, location=Location(1, 1), prefix="", on_completed=None):
4446
args = {"file": path, "line": location.line, "offset": location.offset, "prefix": prefix}
@@ -104,12 +106,14 @@ def open(self, path):
104106
req_dict = self.create_req_dict("open", args)
105107
json_str = json_helpers.encode(req_dict)
106108
self.__comm.postCmd(json_str)
109+
self.__comm.postCmdToWorker(json_str)
107110

108111
def close(self, path):
109112
args = {"file": path}
110113
req_dict = self.create_req_dict("close", args)
111114
json_str = json_helpers.encode(req_dict)
112115
self.__comm.postCmd(json_str)
116+
self.__comm.postCmdToWorker(json_str)
113117

114118
def references(self, path, location=Location(1, 1)):
115119
args = {"file": path, "line": location.line, "offset": location.offset}
@@ -122,14 +126,15 @@ def reload(self, path, alternate_path):
122126
args = {"file": path, "tmpfile": alternate_path}
123127
req_dict = self.create_req_dict("reload", args)
124128
json_str = json_helpers.encode(req_dict)
125-
response_dict = self.__comm.sendCmdSync(json_str, req_dict["seq"])
126-
return response_dict
129+
self.__comm.postCmd(json_str)
130+
self.__comm.postCmdToWorker(json_str)
127131

128132
def reload_async(self, path, alternate_path, on_completed):
129133
args = {"file": path, "tmpfile": alternate_path}
130134
req_dict = self.create_req_dict("reload", args)
131135
json_str = json_helpers.encode(req_dict)
132136
self.__comm.sendCmdAsync(json_str, req_dict["seq"], on_completed)
137+
self.__comm.postCmdToWorker(json_str)
133138

134139
def rename(self, path, location=Location(1, 1)):
135140
args = {"file": path, "line": location.line, "offset": location.offset}
@@ -142,7 +147,7 @@ def request_get_err(self, delay=0, pathList=[]):
142147
args = {"files": pathList, "delay": delay}
143148
req_dict = self.create_req_dict("geterr", args)
144149
json_str = json_helpers.encode(req_dict)
145-
self.__comm.postCmd(json_str)
150+
self.__comm.postCmdToWorker(json_str)
146151

147152
def type(self, path, location=Location(1, 1)):
148153
args = {"file": path, "line": location.line, "offset": location.offset}

0 commit comments

Comments
 (0)