runKernel is the main entry point to start the kernel.
(connectionFile string)
| 125 | |
| 126 | // runKernel is the main entry point to start the kernel. |
| 127 | func runKernel(connectionFile string) { |
| 128 | |
| 129 | // Create a new interpreter for evaluating notebook code. |
| 130 | ir := interp.New() |
| 131 | |
| 132 | // Throw out the error/warning messages that gomacro outputs writes to these streams. |
| 133 | ir.Comp.Stdout = ioutil.Discard |
| 134 | ir.Comp.Stderr = ioutil.Discard |
| 135 | |
| 136 | // Inject the "display" package to render HTML, JSON, PNG, JPEG, SVG... from interpreted code |
| 137 | // maybe a dot-import is easier to use? |
| 138 | display := importPackage(ir, "display", "display") |
| 139 | |
| 140 | // Inject the stub "Display" function. declare a variable |
| 141 | // instead of a function, because we want to later change |
| 142 | // its value to the closure that holds a reference to msgReceipt |
| 143 | ir.DeclVar("Display", nil, stubDisplay) |
| 144 | |
| 145 | // Parse the connection info. |
| 146 | var connInfo ConnectionInfo |
| 147 | |
| 148 | connData, err := ioutil.ReadFile(connectionFile) |
| 149 | if err != nil { |
| 150 | log.Fatal(err) |
| 151 | } |
| 152 | |
| 153 | if err = json.Unmarshal(connData, &connInfo); err != nil { |
| 154 | log.Fatal(err) |
| 155 | } |
| 156 | |
| 157 | // Set up the ZMQ sockets through which the kernel will communicate. |
| 158 | sockets, err := prepareSockets(connInfo) |
| 159 | if err != nil { |
| 160 | log.Fatal(err) |
| 161 | } |
| 162 | |
| 163 | // TODO connect all channel handlers to a WaitGroup to ensure shutdown before returning from runKernel. |
| 164 | |
| 165 | // Start up the heartbeat handler. |
| 166 | startHeartbeat(sockets.HBSocket, &sync.WaitGroup{}) |
| 167 | |
| 168 | // TODO gracefully shutdown the heartbeat handler on kernel shutdown by closing the chan returned by startHeartbeat. |
| 169 | |
| 170 | type msgType struct { |
| 171 | Msg zmq4.Msg |
| 172 | Err error |
| 173 | } |
| 174 | |
| 175 | var ( |
| 176 | shell = make(chan msgType) |
| 177 | stdin = make(chan msgType) |
| 178 | ctl = make(chan msgType) |
| 179 | quit = make(chan int) |
| 180 | ) |
| 181 | |
| 182 | defer close(quit) |
| 183 | poll := func(msgs chan msgType, sck zmq4.Socket) { |
| 184 | defer close(msgs) |