github.com/yunabe/lgo@v0.0.0-20190709125917-42c42d410fdf/examples/interrupt.ipynb (about) 1 { 2 "cells": [ 3 { 4 "cell_type": "markdown", 5 "metadata": {}, 6 "source": [ 7 "# lgo and interrupt\n", 8 "This notebook demonstrates you can interrupts lgo execution in Jupyter notebook.\n", 9 "To interrupt code in Jupyter Notebook, please press the stop button in the toolbar or press `I, I` in command mode." 10 ] 11 }, 12 { 13 "cell_type": "markdown", 14 "metadata": {}, 15 "source": [ 16 "# For-loop" 17 ] 18 }, 19 { 20 "cell_type": "code", 21 "execution_count": null, 22 "metadata": {}, 23 "outputs": [ 24 { 25 "name": "stderr", 26 "output_type": "stream", 27 "text": [ 28 "interrupted" 29 ] 30 }, 31 { 32 "name": "stdout", 33 "output_type": "stream", 34 "text": [ 35 "i = 314720394, sum = 49524463042397421, i*(i-1)/2 = 49524463042397421\n" 36 ] 37 } 38 ], 39 "source": [ 40 "import (\n", 41 " \"fmt\"\n", 42 ")\n", 43 "\n", 44 "var sum, i int64\n", 45 "defer func() {\n", 46 " fmt.Printf(\"i = %d, sum = %d, i*(i-1)/2 = %d\\n\", i, sum, i*(i-1)/2)\n", 47 "}()\n", 48 "for i = int64(0);; i++ {\n", 49 " sum += i\n", 50 "}" 51 ] 52 }, 53 { 54 "cell_type": "markdown", 55 "metadata": {}, 56 "source": [ 57 "# recursion\n", 58 "You can interrupt heavy recursive functions" 59 ] 60 }, 61 { 62 "cell_type": "code", 63 "execution_count": null, 64 "metadata": {}, 65 "outputs": [ 66 { 67 "name": "stdout", 68 "output_type": "stream", 69 "text": [ 70 "time: 13.047495052s\n", 71 "leaf counter: 1483226484\n", 72 "avg: 8ns/cycle\n" 73 ] 74 }, 75 { 76 "name": "stderr", 77 "output_type": "stream", 78 "text": [ 79 "interrupted" 80 ] 81 } 82 ], 83 "source": [ 84 "import (\n", 85 " \"fmt\"\n", 86 " \"time\"\n", 87 ")\n", 88 "\n", 89 "leaf := 0\n", 90 "\n", 91 "func naiveFib(n int64) int64 {\n", 92 " if n < 2 {\n", 93 " leaf++\n", 94 " return 1\n", 95 " }\n", 96 " return naiveFib(n-1) + naiveFib(n-2)\n", 97 "}\n", 98 "\n", 99 "start := time.Now()\n", 100 "defer func(){\n", 101 " end := time.Now()\n", 102 " fmt.Printf(\"time: %v\\n\", end.Sub(start))\n", 103 " fmt.Printf(\"leaf counter: %d\\n\", leaf)\n", 104 " fmt.Printf(\"avg: %v/cycle\\n\", end.Sub(start)/time.Duration(leaf))\n", 105 "}()\n", 106 "naiveFib(50)" 107 ] 108 }, 109 { 110 "cell_type": "markdown", 111 "metadata": {}, 112 "source": [ 113 "# channel and select" 114 ] 115 }, 116 { 117 "cell_type": "code", 118 "execution_count": null, 119 "metadata": {}, 120 "outputs": [ 121 { 122 "name": "stderr", 123 "output_type": "stream", 124 "text": [ 125 "interrupted\n" 126 ] 127 } 128 ], 129 "source": [ 130 "{\n", 131 " c := make(chan int)\n", 132 " // Block forever because no one reads c.\n", 133 " c <- 10\n", 134 "}" 135 ] 136 }, 137 { 138 "cell_type": "code", 139 "execution_count": null, 140 "metadata": {}, 141 "outputs": [ 142 { 143 "name": "stderr", 144 "output_type": "stream", 145 "text": [ 146 "interrupted\n" 147 ] 148 } 149 ], 150 "source": [ 151 "{\n", 152 " c := make(chan int)\n", 153 " // Block forever because no one sends an int to c.\n", 154 " <-c\n", 155 "}" 156 ] 157 }, 158 { 159 "cell_type": "code", 160 "execution_count": null, 161 "metadata": {}, 162 "outputs": [ 163 { 164 "name": "stderr", 165 "output_type": "stream", 166 "text": [ 167 "interrupted\n" 168 ] 169 } 170 ], 171 "source": [ 172 "{\n", 173 " c0, c1 := make(chan int), make(chan int)\n", 174 " // Block forever because no one read or write c0 and c1.\n", 175 " select {\n", 176 " case c0 <- 10:\n", 177 " fmt.Println(\"Sent an int to c0\")\n", 178 " case i := <-c1:\n", 179 " fmt.Println(\"Received\", i)\n", 180 " }\n", 181 "}" 182 ] 183 }, 184 { 185 "cell_type": "markdown", 186 "metadata": {}, 187 "source": [ 188 "# goroutine" 189 ] 190 }, 191 { 192 "cell_type": "code", 193 "execution_count": null, 194 "metadata": {}, 195 "outputs": [ 196 { 197 "name": "stdout", 198 "output_type": "stream", 199 "text": [ 200 "i = 0 (in goroutine)\n" 201 ] 202 } 203 ], 204 "source": [ 205 "import (\n", 206 " \"fmt\"\n", 207 ")\n", 208 "\n", 209 "go func() {\n", 210 " var i int64\n", 211 " defer func() {\n", 212 " fmt.Printf(\"i = %d (in goroutine)\\n\", i)\n", 213 " }()\n", 214 " for i = 0 ;; i++ {}\n", 215 "}()" 216 ] 217 }, 218 { 219 "cell_type": "markdown", 220 "metadata": {}, 221 "source": [ 222 "# Go libraries\n", 223 "lgo creates a special context `_ctx` on every execution and `_ctx` is cancelled when the execution is cancelled. Please pass `_ctx` as a context.Context param of Go libraries you want to cancel (See [README.md](https://github.com/yunabe/lgo/blob/master/README.md#cancellation))." 224 ] 225 }, 226 { 227 "cell_type": "code", 228 "execution_count": null, 229 "metadata": {}, 230 "outputs": [ 231 { 232 "name": "stdout", 233 "output_type": "stream", 234 "text": [ 235 "Failed: Get https://yunabe-codelab.appspot.com/slow?sec=10: context canceled" 236 ] 237 } 238 ], 239 "source": [ 240 "// This demo demostrates how to use net/http with cancellation in lgo.\n", 241 "import (\n", 242 " \"fmt\"\n", 243 " \"io/ioutil\"\n", 244 " \"net/http\"\n", 245 ")\n", 246 "\n", 247 "{\n", 248 " waitSec := 10\n", 249 " var err error\n", 250 " defer func() {\n", 251 " if err != nil {\n", 252 " fmt.Printf(\"Failed: %v\", err)\n", 253 " }\n", 254 " }()\n", 255 " url := fmt.Sprintf(\"https://yunabe-codelab.appspot.com/slow?sec=%d\", waitSec)\n", 256 " req, err := http.NewRequest(\"GET\", url, nil)\n", 257 " if err != nil {\n", 258 " return\n", 259 " }\n", 260 " res, err := http.DefaultClient.Do(req.WithContext(_ctx))\n", 261 " if err != nil {\n", 262 " return\n", 263 " }\n", 264 " b, err := ioutil.ReadAll(res.Body)\n", 265 " if err != nil {\n", 266 " return\n", 267 " }\n", 268 " fmt.Printf(\"Got: %q\", b)\n", 269 "}" 270 ] 271 } 272 ], 273 "metadata": { 274 "kernelspec": { 275 "display_name": "Go (lgo)", 276 "language": "go", 277 "name": "lgo" 278 }, 279 "language_info": { 280 "file_extension": "", 281 "mimetype": "", 282 "name": "go", 283 "version": "" 284 } 285 }, 286 "nbformat": 4, 287 "nbformat_minor": 2 288 }