4
4
import json
5
5
import contextlib
6
6
import io
7
- from threading import Thread
8
7
import traceback
9
8
10
- is_interrupted = False
11
- EXECUTE_QUEUE = []
12
9
STDIN = sys .stdin
13
10
STDOUT = sys .stdout
14
11
STDERR = sys .stderr
@@ -36,29 +33,20 @@ def exec_function(user_input):
36
33
return eval
37
34
38
35
39
- # have to run execute in different thread
40
- # interrupt will kill the thread.
36
+ def execute (request , user_globals ):
37
+ str_output = CustomIO ("<stdout>" , encoding = "utf-8" )
38
+ str_error = CustomIO ("<stderr>" , encoding = "utf-8" )
41
39
40
+ with redirect_io ("stdout" , str_output ):
41
+ with redirect_io ("stderr" , str_error ):
42
+ str_input = CustomIO ("<stdin>" , encoding = "utf-8" , newline = "\n " )
43
+ with redirect_io ("stdin" , str_input ):
44
+ user_output_globals = exec_user_input (request ["params" ], user_globals )
45
+ send_response (str_output .get_value (), request ["id" ])
46
+ user_globals .update (user_output_globals )
42
47
43
- def execute ():
44
- while EXECUTE_QUEUE :
45
- request = EXECUTE_QUEUE .pop (0 )
46
48
47
- str_output = CustomIO ("<stdout>" , encoding = "utf-8" )
48
- str_error = CustomIO ("<stderr>" , encoding = "utf-8" )
49
-
50
- with redirect_io ("stdout" , str_output ):
51
- with redirect_io ("stderr" , str_error ):
52
- str_input = CustomIO ("<stdin>" , encoding = "utf-8" , newline = "\n " )
53
- with redirect_io ("stdin" , str_input ):
54
- user_output_globals = exec_user_input (
55
- request ["id" ], request ["params" ], user_globals
56
- )
57
- send_response (str_output .get_value (), request ["id" ])
58
- user_globals .update (user_output_globals )
59
-
60
-
61
- def exec_user_input (request_id , user_input , user_globals ):
49
+ def exec_user_input (user_input , user_globals ):
62
50
# have to do redirection
63
51
user_input = user_input [0 ] if isinstance (user_input , list ) else user_input
64
52
user_globals = user_globals .copy ()
@@ -68,6 +56,8 @@ def exec_user_input(request_id, user_input, user_globals):
68
56
retval = callable (user_input , user_globals )
69
57
if retval is not None :
70
58
print (retval )
59
+ except KeyboardInterrupt :
60
+ print (traceback .format_exc ())
71
61
except Exception :
72
62
print (traceback .format_exc ())
73
63
return user_globals
@@ -111,41 +101,22 @@ def get_headers():
111
101
return headers
112
102
113
103
114
- # execute_queue.append({"id": 1, "params": "print('hello')"})
115
-
116
104
if __name__ == "__main__" :
117
105
user_globals = {}
118
- thread = None
119
106
120
107
while not STDIN .closed :
121
108
try :
122
109
headers = get_headers ()
123
110
content_length = int (headers .get ("Content-Length" , 0 ))
124
- # just one execute thread
125
- # queue execute items on that thread
111
+
126
112
if content_length :
127
113
request_text = STDIN .read (content_length ) # make sure Im getting right content
128
114
request_json = json .loads (request_text )
129
115
if request_json ["method" ] == "execute" :
130
- EXECUTE_QUEUE .append (request_json )
131
- if thread is None or not thread .is_alive ():
132
- thread = Thread (target = execute )
133
- thread .start ()
134
- # execute_queue.append(request_json) # instead of directly calling execute, create another thread and run execute inside that thread
135
- elif request_json ["method" ] == "interrupt" :
136
- # kill 'thread'
137
- # thread._stop() # THIS IS NOT WORKING
138
-
139
- # set thread as empty
140
- thread = None
141
- # clear execute queue
142
- EXECUTE_QUEUE .clear ()
116
+ execute (request_json , user_globals )
143
117
144
118
elif request_json ["method" ] == "exit" :
145
119
sys .exit (0 )
146
120
147
121
except Exception as e :
148
122
print_log (str (e ))
149
-
150
-
151
- # problem is not able to send interrupt to right thread or kill the thread directly.
0 commit comments