/
futures.ipynb.txt
749 lines (749 loc) · 24.8 KB
/
futures.ipynb.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Custom Workloads with Futures\n",
"=============================\n",
"\n",
"<img src=\"http://dask.readthedocs.io/en/latest/_images/dask_horizontal.svg\" \n",
" width=\"30%\" \n",
" align=right\n",
" alt=\"Dask logo\">\n",
"\n",
"Dask futures provide fine-grained real-time execution for custom situations. This is the foundation for other APIs like Dask arrays and dataframes."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Start Dask Client\n",
"\n",
"Unlike for arrays and dataframes, you need the Dask client to use the Futures interface. Additionally the client provides a dashboard which \n",
"is useful to gain insight on the computation.\n",
"\n",
"The link to the dashboard will become visible when you create the client below. We recommend having it open on one side of your screen while using your notebook on the other side. This can take some effort to arrange your windows, but seeing them both at the same is very useful when learning."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"execution": {
"iopub.execute_input": "2022-07-27T19:12:01.930989Z",
"iopub.status.busy": "2022-07-27T19:12:01.930546Z",
"iopub.status.idle": "2022-07-27T19:12:04.030891Z",
"shell.execute_reply": "2022-07-27T19:12:04.030009Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
" <div style=\"width: 24px; height: 24px; background-color: #e1e1e1; border: 3px solid #9D9D9D; border-radius: 5px; position: absolute;\"> </div>\n",
" <div style=\"margin-left: 48px;\">\n",
" <h3 style=\"margin-bottom: 0px;\">Client</h3>\n",
" <p style=\"color: #9D9D9D; margin-bottom: 0px;\">Client-fee34b1a-0ddf-11ed-9a76-000d3a8f7959</p>\n",
" <table style=\"width: 100%; text-align: left;\">\n",
"\n",
" <tr>\n",
" \n",
" <td style=\"text-align: left;\"><strong>Connection method:</strong> Cluster object</td>\n",
" <td style=\"text-align: left;\"><strong>Cluster type:</strong> distributed.LocalCluster</td>\n",
" \n",
" </tr>\n",
"\n",
" \n",
" <tr>\n",
" <td style=\"text-align: left;\">\n",
" <strong>Dashboard: </strong> <a href=\"http://127.0.0.1:8787/status\" target=\"_blank\">http://127.0.0.1:8787/status</a>\n",
" </td>\n",
" <td style=\"text-align: left;\"></td>\n",
" </tr>\n",
" \n",
"\n",
" </table>\n",
"\n",
" \n",
" <details>\n",
" <summary style=\"margin-bottom: 20px;\"><h3 style=\"display: inline;\">Cluster Info</h3></summary>\n",
" <div class=\"jp-RenderedHTMLCommon jp-RenderedHTML jp-mod-trusted jp-OutputArea-output\">\n",
" <div style=\"width: 24px; height: 24px; background-color: #e1e1e1; border: 3px solid #9D9D9D; border-radius: 5px; position: absolute;\">\n",
" </div>\n",
" <div style=\"margin-left: 48px;\">\n",
" <h3 style=\"margin-bottom: 0px; margin-top: 0px;\">LocalCluster</h3>\n",
" <p style=\"color: #9D9D9D; margin-bottom: 0px;\">cb0544e0</p>\n",
" <table style=\"width: 100%; text-align: left;\">\n",
" <tr>\n",
" <td style=\"text-align: left;\">\n",
" <strong>Dashboard:</strong> <a href=\"http://127.0.0.1:8787/status\" target=\"_blank\">http://127.0.0.1:8787/status</a>\n",
" </td>\n",
" <td style=\"text-align: left;\">\n",
" <strong>Workers:</strong> 1\n",
" </td>\n",
" </tr>\n",
" <tr>\n",
" <td style=\"text-align: left;\">\n",
" <strong>Total threads:</strong> 4\n",
" </td>\n",
" <td style=\"text-align: left;\">\n",
" <strong>Total memory:</strong> 6.78 GiB\n",
" </td>\n",
" </tr>\n",
" \n",
" <tr>\n",
" <td style=\"text-align: left;\"><strong>Status:</strong> running</td>\n",
" <td style=\"text-align: left;\"><strong>Using processes:</strong> True</td>\n",
"</tr>\n",
"\n",
" \n",
" </table>\n",
"\n",
" <details>\n",
" <summary style=\"margin-bottom: 20px;\">\n",
" <h3 style=\"display: inline;\">Scheduler Info</h3>\n",
" </summary>\n",
"\n",
" <div style=\"\">\n",
" <div>\n",
" <div style=\"width: 24px; height: 24px; background-color: #FFF7E5; border: 3px solid #FF6132; border-radius: 5px; position: absolute;\"> </div>\n",
" <div style=\"margin-left: 48px;\">\n",
" <h3 style=\"margin-bottom: 0px;\">Scheduler</h3>\n",
" <p style=\"color: #9D9D9D; margin-bottom: 0px;\">Scheduler-8e5a5d68-572d-407a-addc-5d81df8d4c5f</p>\n",
" <table style=\"width: 100%; text-align: left;\">\n",
" <tr>\n",
" <td style=\"text-align: left;\">\n",
" <strong>Comm:</strong> tcp://127.0.0.1:38745\n",
" </td>\n",
" <td style=\"text-align: left;\">\n",
" <strong>Workers:</strong> 1\n",
" </td>\n",
" </tr>\n",
" <tr>\n",
" <td style=\"text-align: left;\">\n",
" <strong>Dashboard:</strong> <a href=\"http://127.0.0.1:8787/status\" target=\"_blank\">http://127.0.0.1:8787/status</a>\n",
" </td>\n",
" <td style=\"text-align: left;\">\n",
" <strong>Total threads:</strong> 4\n",
" </td>\n",
" </tr>\n",
" <tr>\n",
" <td style=\"text-align: left;\">\n",
" <strong>Started:</strong> Just now\n",
" </td>\n",
" <td style=\"text-align: left;\">\n",
" <strong>Total memory:</strong> 6.78 GiB\n",
" </td>\n",
" </tr>\n",
" </table>\n",
" </div>\n",
" </div>\n",
"\n",
" <details style=\"margin-left: 48px;\">\n",
" <summary style=\"margin-bottom: 20px;\">\n",
" <h3 style=\"display: inline;\">Workers</h3>\n",
" </summary>\n",
"\n",
" \n",
" <div style=\"margin-bottom: 20px;\">\n",
" <div style=\"width: 24px; height: 24px; background-color: #DBF5FF; border: 3px solid #4CC9FF; border-radius: 5px; position: absolute;\"> </div>\n",
" <div style=\"margin-left: 48px;\">\n",
" <details>\n",
" <summary>\n",
" <h4 style=\"margin-bottom: 0px; display: inline;\">Worker: 0</h4>\n",
" </summary>\n",
" <table style=\"width: 100%; text-align: left;\">\n",
" <tr>\n",
" <td style=\"text-align: left;\">\n",
" <strong>Comm: </strong> tcp://127.0.0.1:37435\n",
" </td>\n",
" <td style=\"text-align: left;\">\n",
" <strong>Total threads: </strong> 4\n",
" </td>\n",
" </tr>\n",
" <tr>\n",
" <td style=\"text-align: left;\">\n",
" <strong>Dashboard: </strong> <a href=\"http://127.0.0.1:36253/status\" target=\"_blank\">http://127.0.0.1:36253/status</a>\n",
" </td>\n",
" <td style=\"text-align: left;\">\n",
" <strong>Memory: </strong> 6.78 GiB\n",
" </td>\n",
" </tr>\n",
" <tr>\n",
" <td style=\"text-align: left;\">\n",
" <strong>Nanny: </strong> tcp://127.0.0.1:33885\n",
" </td>\n",
" <td style=\"text-align: left;\"></td>\n",
" </tr>\n",
" <tr>\n",
" <td colspan=\"2\" style=\"text-align: left;\">\n",
" <strong>Local directory: </strong> /home/runner/work/dask-examples/dask-examples/dask-worker-space/worker-5e0um0el\n",
" </td>\n",
" </tr>\n",
"\n",
" \n",
"\n",
" \n",
"\n",
" </table>\n",
" </details>\n",
" </div>\n",
" </div>\n",
" \n",
"\n",
" </details>\n",
"</div>\n",
"\n",
" </details>\n",
" </div>\n",
"</div>\n",
" </details>\n",
" \n",
"\n",
" </div>\n",
"</div>"
],
"text/plain": [
"<Client: 'tcp://127.0.0.1:38745' processes=1 threads=4, memory=6.78 GiB>"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from dask.distributed import Client, progress\n",
"client = Client(threads_per_worker=4, n_workers=1)\n",
"client"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Create simple functions\n",
"\n",
"These functions do simple operations like add two numbers together, but they sleep for a random amount of time to simulate real work."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"execution": {
"iopub.execute_input": "2022-07-27T19:12:04.035640Z",
"iopub.status.busy": "2022-07-27T19:12:04.035423Z",
"iopub.status.idle": "2022-07-27T19:12:04.041328Z",
"shell.execute_reply": "2022-07-27T19:12:04.040706Z"
}
},
"outputs": [],
"source": [
"import time\n",
"import random\n",
"\n",
"def inc(x):\n",
" time.sleep(random.random())\n",
" return x + 1\n",
"\n",
"def double(x):\n",
" time.sleep(random.random())\n",
" return 2 * x\n",
" \n",
"def add(x, y):\n",
" time.sleep(random.random())\n",
" return x + y "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can run them locally"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"execution": {
"iopub.execute_input": "2022-07-27T19:12:04.044088Z",
"iopub.status.busy": "2022-07-27T19:12:04.043676Z",
"iopub.status.idle": "2022-07-27T19:12:04.118612Z",
"shell.execute_reply": "2022-07-27T19:12:04.117951Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"inc(1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Or we can submit them to run remotely with Dask. This immediately returns a future that points to the ongoing computation, and eventually to the stored result."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"execution": {
"iopub.execute_input": "2022-07-27T19:12:04.121736Z",
"iopub.status.busy": "2022-07-27T19:12:04.121526Z",
"iopub.status.idle": "2022-07-27T19:12:04.282601Z",
"shell.execute_reply": "2022-07-27T19:12:04.281961Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"<strong>Future: inc</strong>\n",
"<span style=\"color: var(--jp-ui-font-color2, gray)\"> status: </span>\n",
"\n",
"\n",
"<span style=\"color: var(--jp-error-color0, black)\">pending</span>,\n",
"\n",
"\n",
"\n",
"<span style=\"color: var(--jp-ui-font-color2, gray)\"> type:</span> NoneType,\n",
"\n",
"\n",
"<span style=\"color: var(--jp-ui-font-color2, gray)\"> key:</span> inc-6a39f735d591f959f1c67a637717a113"
],
"text/plain": [
"<Future: pending, key: inc-6a39f735d591f959f1c67a637717a113>"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"future = client.submit(inc, 1) # returns immediately with pending future\n",
"future"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you wait a second, and then check on the future again, you'll see that it has finished."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"execution": {
"iopub.execute_input": "2022-07-27T19:12:04.285633Z",
"iopub.status.busy": "2022-07-27T19:12:04.285234Z",
"iopub.status.idle": "2022-07-27T19:12:04.292631Z",
"shell.execute_reply": "2022-07-27T19:12:04.292010Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"<strong>Future: inc</strong>\n",
"<span style=\"color: var(--jp-ui-font-color2, gray)\"> status: </span>\n",
"\n",
"\n",
"<span style=\"color: var(--jp-error-color0, black)\">pending</span>,\n",
"\n",
"\n",
"\n",
"<span style=\"color: var(--jp-ui-font-color2, gray)\"> type:</span> NoneType,\n",
"\n",
"\n",
"<span style=\"color: var(--jp-ui-font-color2, gray)\"> key:</span> inc-6a39f735d591f959f1c67a637717a113"
],
"text/plain": [
"<Future: pending, key: inc-6a39f735d591f959f1c67a637717a113>"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"future # scheduler and client talk constantly"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can block on the computation and gather the result with the `.result()` method."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"execution": {
"iopub.execute_input": "2022-07-27T19:12:04.295639Z",
"iopub.status.busy": "2022-07-27T19:12:04.295233Z",
"iopub.status.idle": "2022-07-27T19:12:05.154578Z",
"shell.execute_reply": "2022-07-27T19:12:05.153941Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"future.result()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Chain dependencies\n",
"\n",
"You can submit tasks on other futures. This will create a dependency between the inputs and outputs. Dask will track the execution of all tasks, ensuring that downstream tasks are run at the proper time and place and with the proper data."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"execution": {
"iopub.execute_input": "2022-07-27T19:12:05.157907Z",
"iopub.status.busy": "2022-07-27T19:12:05.157510Z",
"iopub.status.idle": "2022-07-27T19:12:05.178211Z",
"shell.execute_reply": "2022-07-27T19:12:05.177620Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"<strong>Future: add</strong>\n",
"<span style=\"color: var(--jp-ui-font-color2, gray)\"> status: </span>\n",
"\n",
"\n",
"<span style=\"color: var(--jp-error-color0, black)\">pending</span>,\n",
"\n",
"\n",
"\n",
"<span style=\"color: var(--jp-ui-font-color2, gray)\"> type:</span> NoneType,\n",
"\n",
"\n",
"<span style=\"color: var(--jp-ui-font-color2, gray)\"> key:</span> add-a255b15196528b8a69e0ea73bff2be61"
],
"text/plain": [
"<Future: pending, key: add-a255b15196528b8a69e0ea73bff2be61>"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = client.submit(inc, 1)\n",
"y = client.submit(double, 2)\n",
"z = client.submit(add, x, y)\n",
"z"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"execution": {
"iopub.execute_input": "2022-07-27T19:12:05.181535Z",
"iopub.status.busy": "2022-07-27T19:12:05.181130Z",
"iopub.status.idle": "2022-07-27T19:12:05.366548Z",
"shell.execute_reply": "2022-07-27T19:12:05.365814Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"z.result()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that we never blocked on `x` or `y` nor did we ever have to move their data back to our notebook."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Submit many tasks\n",
"\n",
"So we've learned how to run Python functions remotely. This becomes useful when we add two things:\n",
"\n",
"1. We can submit thousands of tasks per second\n",
"2. Tasks can depend on each other by consuming futures as inputs\n",
"\n",
"We submit many tasks that depend on each other in a normal Python for loop"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"execution": {
"iopub.execute_input": "2022-07-27T19:12:05.369907Z",
"iopub.status.busy": "2022-07-27T19:12:05.369532Z",
"iopub.status.idle": "2022-07-27T19:12:05.374612Z",
"shell.execute_reply": "2022-07-27T19:12:05.373913Z"
}
},
"outputs": [],
"source": [
"zs = []"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"execution": {
"iopub.execute_input": "2022-07-27T19:12:05.377963Z",
"iopub.status.busy": "2022-07-27T19:12:05.377482Z",
"iopub.status.idle": "2022-07-27T19:12:07.984679Z",
"shell.execute_reply": "2022-07-27T19:12:07.983989Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 2.58 s, sys: 92.2 ms, total: 2.68 s\n",
"Wall time: 2.57 s\n"
]
}
],
"source": [
"%%time\n",
"\n",
"for i in range(256):\n",
" x = client.submit(inc, i) # x = inc(i)\n",
" y = client.submit(double, x) # y = inc(x)\n",
" z = client.submit(add, x, y) # z = inc(y)\n",
" zs.append(z)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"execution": {
"iopub.execute_input": "2022-07-27T19:12:07.987756Z",
"iopub.status.busy": "2022-07-27T19:12:07.987552Z",
"iopub.status.idle": "2022-07-27T19:12:08.004144Z",
"shell.execute_reply": "2022-07-27T19:12:08.002120Z"
}
},
"outputs": [],
"source": [
"total = client.submit(sum, zs)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To make this go faster, add an additional workers with more cores \n",
"\n",
"(although we're still only working on our local machine, this is more practical when using an actual cluster)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"execution": {
"iopub.execute_input": "2022-07-27T19:12:08.031784Z",
"iopub.status.busy": "2022-07-27T19:12:08.030743Z",
"iopub.status.idle": "2022-07-27T19:12:08.039918Z",
"shell.execute_reply": "2022-07-27T19:12:08.038967Z"
}
},
"outputs": [],
"source": [
"client.cluster.scale(10) # ask for ten 4-thread workers"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Custom computation: Tree summation\n",
"\n",
"As an example of a non-trivial algorithm, consider the classic tree reduction. We accomplish this with a nested for loop and a bit of normal Python logic.\n",
"\n",
"```\n",
"finish total single output\n",
" ^ / \\\n",
" | c1 c2 neighbors merge\n",
" | / \\ / \\\n",
" | b1 b2 b3 b4 neighbors merge\n",
" ^ / \\ / \\ / \\ / \\\n",
"start a1 a2 a3 a4 a5 a6 a7 a8 many inputs\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"execution": {
"iopub.execute_input": "2022-07-27T19:12:08.045027Z",
"iopub.status.busy": "2022-07-27T19:12:08.044612Z",
"iopub.status.idle": "2022-07-27T19:12:08.925807Z",
"shell.execute_reply": "2022-07-27T19:12:08.924848Z"
}
},
"outputs": [],
"source": [
"L = zs\n",
"while len(L) > 1:\n",
" new_L = []\n",
" for i in range(0, len(L), 2):\n",
" future = client.submit(add, L[i], L[i + 1]) # add neighbors\n",
" new_L.append(future)\n",
" L = new_L # swap old list for new"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you're watching the [dashboard's status page](http://localhost:8787/status) then you may want to note two things:\n",
"\n",
"1. The red bars are for inter-worker communication. They happen as different workers need to combine their intermediate values\n",
"2. There is lots of parallelism at the beginning but less towards the end as we reach the top of the tree where there is less work to do.\n",
"\n",
"Alternatively you may want to navigate to the [dashboard's graph page](http://localhost:8787/graph) and then run the cell above again. You will be able to see the task graph evolve during the computation."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Building a computation dynamically\n",
"----------------------------------\n",
"\n",
"In the examples above we explicitly specify the task graph ahead of time. We know for example that the first two futures in the list `L` will be added together. \n",
"\n",
"Sometimes this isn't always best though, sometimes you want to dynamically define a computation as it is happening. For example we might want to sum up these values based on whichever futures show up first, rather than the order in which they were placed in the list to start with.\n",
"\n",
"For this, we can use operations like [as_completed](http://dask.pydata.org/en/latest/futures.html#distributed.as_completed).\n",
"\n",
"We recommend watching the dashboard's graph page when running this computation. You should see the graph construct itself during execution."
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"execution": {
"iopub.execute_input": "2022-07-27T19:12:08.936475Z",
"iopub.status.busy": "2022-07-27T19:12:08.936061Z",
"iopub.status.idle": "2022-07-27T19:12:08.949152Z",
"shell.execute_reply": "2022-07-27T19:12:08.944342Z"
}
},
"outputs": [],
"source": [
"del future, L, new_L, total # clear out some old work"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"execution": {
"iopub.execute_input": "2022-07-27T19:12:08.974825Z",
"iopub.status.busy": "2022-07-27T19:12:08.969342Z",
"iopub.status.idle": "2022-07-27T19:12:31.392724Z",
"shell.execute_reply": "2022-07-27T19:12:31.392112Z"
}
},
"outputs": [],
"source": [
"from dask.distributed import as_completed\n",
"\n",
"zs = client.map(inc, zs)\n",
"seq = as_completed(zs)\n",
"\n",
"while seq.count() > 1: # at least two futures left\n",
" a = next(seq)\n",
" b = next(seq)\n",
" new = client.submit(add, a, b, priority=1) # add them together\n",
" seq.add(new) # add new future back into loop"
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.12"
}
},
"nbformat": 4,
"nbformat_minor": 4
}