@@ -45,29 +45,27 @@ def sendCmdToWorkerAsync(self, cmd, cb): pass
45
45
46
46
class NodeCommClient (CommClient ):
47
47
__CONTENT_LENGTH_HEADER = b"Content-Length: "
48
+ stop_worker = False
48
49
49
- def __init__ (self , script_path ):
50
+ def __init__ (self , scriptPath ):
50
51
"""
51
52
Starts a node client (if not already started) and communicate with it.
52
53
The script file to run is passed to the constructor.
53
54
"""
54
55
55
56
self .asyncReq = {}
56
57
self .__serverProc = None
57
- self .script_path = script_path
58
+ self .__workerProc = None
59
+ self .script_path = scriptPath
58
60
59
61
# create response and event queues
60
62
self .__msgq = queue .Queue ()
61
63
self .__eventq = queue .Queue ()
62
64
self .__worker_eventq = queue .Queue ()
63
65
64
- # set worker to None at init
65
- self .__workerProc = None
66
-
67
66
# start node process
68
67
pref_settings = sublime .load_settings ('Preferences.sublime-settings' )
69
68
node_path = pref_settings .get ('node_path' )
70
- project_error_list_enabled = pref_settings .get ('project_error_list' )
71
69
if node_path :
72
70
node_path = os .path .expandvars (node_path )
73
71
if not node_path :
@@ -89,26 +87,29 @@ def __init__(self, script_path):
89
87
# so only use it if on Windows
90
88
si = subprocess .STARTUPINFO ()
91
89
si .dwFlags |= subprocess .SW_HIDE | subprocess .STARTF_USESHOWWINDOW
92
- self .__serverProc = subprocess .Popen ([node_path , script_path ],
90
+ self .__serverProc = subprocess .Popen ([node_path , scriptPath ],
93
91
stdin = subprocess .PIPE , stdout = subprocess .PIPE , startupinfo = si )
94
92
else :
95
- log .debug ("opening " + node_path + " " + script_path )
96
- self .__serverProc = subprocess .Popen ([node_path , script_path ],
93
+ log .debug ("opening " + node_path + " " + scriptPath )
94
+ self .__serverProc = subprocess .Popen ([node_path , scriptPath ],
97
95
stdin = subprocess .PIPE , stdout = subprocess .PIPE )
98
96
except :
99
97
self .__serverProc = None
100
98
# start reader thread
101
99
if self .__serverProc and (not self .__serverProc .poll ()):
102
100
log .debug ("server proc " + str (self .__serverProc ))
103
101
log .debug ("starting reader thread" )
104
- readerThread = threading .Thread (target = NodeCommClient .__reader , args = (self .__serverProc .stdout , self .__msgq , self .__eventq , self .asyncReq , self .__serverProc ))
102
+ readerThread = threading .Thread (target = NodeCommClient .__reader , args = (
103
+ self .__serverProc .stdout , self .__msgq , self .__eventq , self .asyncReq , self .__serverProc ))
105
104
readerThread .daemon = True
106
105
readerThread .start ()
107
106
108
107
self .__debugProc = None
109
108
self .__breakpoints = []
110
109
111
110
def startWorker (self ):
111
+ NodeCommClient .stop_worker = False
112
+
112
113
node_path = global_vars .get_node_path ()
113
114
if os .name == "nt" :
114
115
si = subprocess .STARTUPINFO ()
@@ -120,14 +121,18 @@ def startWorker(self):
120
121
self .__workerProc = subprocess .Popen (
121
122
[node_path , self .script_path ], stdin = subprocess .PIPE , stdout = subprocess .PIPE )
122
123
123
- # start the worker thread
124
- if self .__workerProc and not self .__workerProc .poll ():
125
- log .debug ("worker proc {0}" .format (self .__workerProc ))
126
- log .debug ("starting worker thread" )
127
- self .worker_thread = threading .Thread (target = NodeCommClient .__reader ,
128
- args = (self .__workerProc .stdout , self .__msgq , self .__worker_eventq , self .asyncReq , self .__workerProc ))
129
- self .worker_thread .daemon = True
130
- self .worker_thread .start ()
124
+ # start reader thread
125
+ if self .__serverProc and (not self .__serverProc .poll ()):
126
+ log .debug ("server proc " + str (self .__serverProc ))
127
+ log .debug ("starting reader thread" )
128
+ readerThread = threading .Thread (target = NodeCommClient .__reader , args = (
129
+ self .__serverProc .stdout , self .__msgq , self .__eventq , self .asyncReq , self .__serverProc ))
130
+ readerThread .daemon = True
131
+ readerThread .start ()
132
+ log .debug ("readerThread.is_alive: {0}" .format (readerThread .is_alive ()))
133
+
134
+ def stopWorker (self ):
135
+ NodeCommClient .stop_worker = True
131
136
132
137
def serverStarted (self ):
133
138
return self .__serverProc is not None
@@ -289,7 +294,6 @@ def __readMsg(stream, msgq, eventq, asyncReq, proc):
289
294
# header if header else 'None'
290
295
# )
291
296
# )
292
-
293
297
if len (header ) == 0 :
294
298
if state == 'init' :
295
299
# log.info('0 byte line in stream when expecting header')
@@ -326,11 +330,18 @@ def __readMsg(stream, msgq, eventq, asyncReq, proc):
326
330
327
331
@staticmethod
328
332
def __reader (stream , msgq , eventq , asyncReq , proc ):
329
- """
330
- Main function for reader thread
331
- """
333
+ """ Main function for reader thread """
332
334
while True :
333
335
if NodeCommClient .__readMsg (stream , msgq , eventq , asyncReq , proc ):
336
+ log .debug ("server exited" )
337
+ return
338
+
339
+ @staticmethod
340
+ def __worker_reader (stream , msgq , eventq , asyncReq , proc ):
341
+ """ Main function for worker thread """
342
+ while True :
343
+ if NodeCommClient .__readMsg (stream , msgq , eventq , asyncReq , proc ) or NodeCommClient .stop_worker :
344
+ log .debug ("worker exited" )
334
345
return
335
346
336
347
@staticmethod
0 commit comments