Skip to content

Commit 6373ea7

Browse files
committed
changes to prototype code
1 parent 3ba59b0 commit 6373ea7

2 files changed

Lines changed: 73 additions & 36 deletions

File tree

pythonFiles/PythonTools/ipythonServer.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,59 @@ def release(self):
119119
self.lock.release()
120120

121121

122+
"""
123+
- Add a list with the command ids
124+
- Create a common lock object to be shared between iPythonKernelResponseMonitor and iPythonSocketServer
125+
- iPythonSocketServer will grab a lock and insert an item into the command ids and then send the code for execution
126+
- iPythonKernelResponseMonitor will keep going in a nutsy loop and when it gets a message for execution it will take the oldest item from the loop (pop)
127+
- and send a socket message providing the information (this way we can tie the code to be executed with the message ids)
128+
- Not the best, but that's how it will have to be done
129+
- http://www.xavierdupre.fr/app/pyquickhelper/helpsphinx/_modules/pyquickhelper/ipythonhelper/notebook_runner.html
130+
"""
131+
class iPythonKernelResponseMonitor(object):
132+
def __init__(self, kernelUUID, socketConnection):
133+
import threading
134+
self.kernel = multiKernelManager.get_kernel(kernelUUID)
135+
self.conn = socketConnection
136+
self.is_stop_requested = False
137+
thread.start_new_thread(self.start_processing, ())
138+
139+
def stop(self):
140+
self.is_stop_requested = True
141+
142+
def check_for_exit_socket_loop(self):
143+
return self.is_stop_requested
144+
145+
def start_processing(self):
146+
"""loop to read the io ports/messages"""
147+
148+
_debug_write('Started processing thread')
149+
try:
150+
while True:
151+
if self.check_for_exit_socket_loop():
152+
break
153+
except socket.timeout:
154+
pass
155+
156+
except IPythonExitException:
157+
_debug_write('IPythonExitException')
158+
_debug_write(traceback.format_exc())
159+
pass
160+
except socket.error:
161+
_debug_write('socket error')
162+
_debug_write(traceback.format_exc())
163+
pass
164+
except:
165+
print('crap')
166+
_debug_write('error in repl loop')
167+
_debug_write(traceback.format_exc())
168+
169+
# try and exit gracefully, then interrupt main if necessary
170+
time.sleep(2)
171+
traceback.print_exc()
172+
self.exit_process()
173+
174+
122175
class iPythonSocketServer(object):
123176
"""back end for executing REPL code. This base class handles all of the
124177
communication with the remote process while derived classes implement the

pythonFiles/PythonTools/jupyterTest.py

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -115,30 +115,7 @@ def _check_ipynb():
115115
errors = 0
116116

117117
report = ''
118-
try:
119-
test_results = _execute_cell("print('Hello World')", shell, iopub)
120-
except RuntimeError as e:
121-
report += ('{!s} in cell number: {}'
122-
.format(e, 'cell.prompt_number'))
123-
errors += 1
124-
print('crap execute')
125-
126-
try:
127-
str_test_results = [
128-
'(for out {})\n'.format(k) + '\n'.join(
129-
[' : '.join([str(key), str(val)])
130-
for key, val in t.items()
131-
if key not in ('metadata', 'png')]
132-
) for k, t in enumerate(test_results)]
133-
except:
134-
print('crap')
135-
else:
136-
report += '\n' * 2 + '~' * 40
137-
report += (
138-
'\nFailure in {}:{}\nGot: {}'
139-
).format('notebook.metadata.name',
140-
'cell.prompt_number',
141-
'\n'.join(str_test_results))
118+
_execute_cell("print('Hello World')", shell, iopub, timeout=1)
142119

143120
kernel_client.stop_channels()
144121
kernel_manager.shutdown_kernel()
@@ -171,30 +148,37 @@ def _execute_cell(code, shell, iopub, timeout=300):
171148
"""
172149

173150
# Execute input
174-
shell.execute(code)
175-
exe_result = shell.get_shell_msg(timeout=timeout)
176-
print('exe_result')
177-
print(exe_result)
178-
print('')
179-
if exe_result['content']['status'] == 'error':
180-
raise RuntimeError('Failed to execute cell due to error: {!r}'.format(
181-
str(exe_result['content']['evalue'])))
151+
#shell.execute("10+20")
152+
shell.execute("import time\ntime.sleep(10)\nprint(112341234)")
153+
shell.execute("1+2")
182154

183155
cell_outputs = list()
184156

185157
# Poll for iopub messages until no more messages are available
186158
while True:
159+
try:
160+
exe_result = shell.get_shell_msg(timeout=timeout)
161+
print('exe_result')
162+
print(exe_result)
163+
print('')
164+
if exe_result['content']['status'] == 'error':
165+
print('-----------------------crap---------------------')
166+
raise RuntimeError('Failed to execute cell due to error: {!r}'.format(
167+
str(exe_result['content']['evalue'])))
168+
except Empty:
169+
print('quue empty, try again')
170+
pass
171+
172+
print('\n\n-------------------------------------------------\ntrying\n----------------------------------\n')
187173
try:
188174
msg = iopub.get_iopub_msg(timeout=0.5)
189175
print('get_iopub_msg')
190176
print('msg')
191177
print(msg)
192178
print('')
193179
except Empty:
194-
print('get_iopub_msg')
195-
print('done')
196-
print('')
197-
break
180+
print('get_iopub_msg is empty--------------------------------------')
181+
pass
198182

199183
msg_type = msg['msg_type']
200184
if msg_type in ('status', 'pyin', 'execute_input', 'execute_result'):

0 commit comments

Comments
 (0)