pvanand commited on
Commit
8bf6cc4
·
verified ·
1 Parent(s): f5ef0eb

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +7 -9
main.py CHANGED
@@ -69,7 +69,7 @@ async def kill_kernel(session_token: str):
69
  # Function to execute code in a kernel
70
  async def execute_code(session_token: str, code: str):
71
  setup_code = "%matplotlib inline"
72
-
73
  if session_token not in kernel_managers:
74
  await create_kernel(session_token)
75
 
@@ -85,15 +85,12 @@ async def execute_code(session_token: str, code: str):
85
  output = []
86
  timeout = datetime.now() + timedelta(seconds=TIMEOUT_DURATION)
87
 
 
88
  while True:
89
  if datetime.now() > timeout:
90
  raise TimeoutError("Code execution timed out.")
91
-
92
- try:
93
- msg = kc.get_iopub_msg(timeout=1)
94
- except Empty:
95
- continue
96
-
97
  if msg['msg_type'] == 'status' and msg['content']['execution_state'] == 'idle':
98
  break
99
  elif msg['msg_type'] == 'error':
@@ -103,15 +100,16 @@ async def execute_code(session_token: str, code: str):
103
  "traceback": msg['content']['traceback']
104
  }
105
  output.append({"error": error_output})
106
- elif 'data' in msg['content']:
107
  output.append({"data": msg['content']['data']})
108
  elif 'text' in msg['content']:
109
  output.append({"text": msg['content']['text']})
110
-
111
  last_access_times[session_token] = datetime.now()
112
  return {'status': 'success', 'value': output}
113
 
114
  except Exception as e:
 
115
  return {'status': 'error', 'value': str(e)}
116
 
117
 
 
69
  # Function to execute code in a kernel
70
  async def execute_code(session_token: str, code: str):
71
  setup_code = "%matplotlib inline"
72
+
73
  if session_token not in kernel_managers:
74
  await create_kernel(session_token)
75
 
 
85
  output = []
86
  timeout = datetime.now() + timedelta(seconds=TIMEOUT_DURATION)
87
 
88
+ output = []
89
  while True:
90
  if datetime.now() > timeout:
91
  raise TimeoutError("Code execution timed out.")
92
+
93
+ msg = st.session_state.kernel_client.get_iopub_msg()
 
 
 
 
94
  if msg['msg_type'] == 'status' and msg['content']['execution_state'] == 'idle':
95
  break
96
  elif msg['msg_type'] == 'error':
 
100
  "traceback": msg['content']['traceback']
101
  }
102
  output.append({"error": error_output})
103
+ if 'data' in msg['content']:
104
  output.append({"data": msg['content']['data']})
105
  elif 'text' in msg['content']:
106
  output.append({"text": msg['content']['text']})
107
+
108
  last_access_times[session_token] = datetime.now()
109
  return {'status': 'success', 'value': output}
110
 
111
  except Exception as e:
112
+ last_access_times[session_token] = datetime.now()
113
  return {'status': 'error', 'value': str(e)}
114
 
115