(kc, tries=1, timeout=0.2)
| 142 | |
| 143 | |
| 144 | def flush_kernel_msgs(kc, tries=1, timeout=0.2): |
| 145 | try: |
| 146 | hit_empty = 0 |
| 147 | |
| 148 | while True: |
| 149 | try: |
| 150 | msg = kc.get_iopub_msg(timeout=timeout) |
| 151 | if msg["msg_type"] == "execute_result": |
| 152 | if "text/plain" in msg["content"]["data"]: |
| 153 | send_message( |
| 154 | msg["content"]["data"]["text/plain"], "message_raw" |
| 155 | ) |
| 156 | if msg["msg_type"] == "display_data": |
| 157 | if "image/png" in msg["content"]["data"]: |
| 158 | # Convert to Slack upload |
| 159 | send_message( |
| 160 | msg["content"]["data"]["image/png"], |
| 161 | message_type="image/png", |
| 162 | ) |
| 163 | elif "text/plain" in msg["content"]["data"]: |
| 164 | send_message(msg["content"]["data"]["text/plain"]) |
| 165 | |
| 166 | elif msg["msg_type"] == "stream": |
| 167 | logger.debug("Received stream output %s" % msg["content"]["text"]) |
| 168 | send_message(msg["content"]["text"]) |
| 169 | elif msg["msg_type"] == "error": |
| 170 | send_message( |
| 171 | utils.escape_ansi("\n".join(msg["content"]["traceback"])), |
| 172 | "message_raw", |
| 173 | ) |
| 174 | except queue.Empty: |
| 175 | hit_empty += 1 |
| 176 | if hit_empty == tries: |
| 177 | # Empty queue for one second, give back control |
| 178 | break |
| 179 | except (ValueError, IndexError): |
| 180 | # get_iopub_msg suffers from message fetch errors |
| 181 | break |
| 182 | except Exception as e: |
| 183 | logger.info(f"{e} [{type(e)}") |
| 184 | logger.info(traceback.format_exc()) |
| 185 | break |
| 186 | except Exception as e: |
| 187 | logger.info(f"{e} [{type(e)}") |
| 188 | |
| 189 | |
| 190 | def start_kernel(): |
no test coverage detected