1 00:00:05,940 --> 00:00:10,470 Let's take a look at how we can use tasks to run our futures. 2 00:00:10,830 --> 00:00:19,140 So inside of Main, we're going to say let task equals task spawn. 3 00:00:20,370 --> 00:00:23,670 And now we're going to insert an async code block. 4 00:00:25,170 --> 00:00:28,830 And then inside this code block, we're going to say let result equals. 5 00:00:28,830 --> 00:00:34,800 And we want to call our read file function and we'll call, read text. 6 00:00:35,880 --> 00:00:40,080 And now we want to wait for the future result to be returned. 7 00:00:40,080 --> 00:00:47,490 And I went ahead and created a text file and I just gave it some contents that say file to read from. 8 00:00:47,490 --> 00:00:48,900 So something super simple. 9 00:00:50,430 --> 00:00:53,250 So now we can come back here and we can rematch on. 10 00:00:53,990 --> 00:00:58,670 Result because we are returning a result from our function. 11 00:00:59,060 --> 00:01:03,800 So in the event that everything is okay, we want to return. 12 00:01:05,550 --> 00:01:12,480 And print out the contents of what was returned to us. 13 00:01:14,470 --> 00:01:19,690 And then if we get an error, obviously we want to print out what our heir was. 14 00:01:21,150 --> 00:01:26,880 So we're reading from file. 15 00:01:27,240 --> 00:01:31,200 And now we want to print out our air. 16 00:01:33,220 --> 00:01:34,000 So. 17 00:01:35,040 --> 00:01:37,950 Spawn takes a future and starts running. 18 00:01:38,760 --> 00:01:39,850 Dawn a task. 19 00:01:40,150 --> 00:01:45,700 And we can see that it returns a joint handle, just like we saw in our concurrency section. 20 00:01:45,700 --> 00:01:51,390 So a task is very similar to a thread with some very minor differences. 21 00:01:51,400 --> 00:01:58,300 The task will be scheduled by the program, and if the task has to wait, the program is responsible 22 00:01:58,300 --> 00:01:59,770 for waking it back up. 23 00:02:00,500 --> 00:02:03,350 We also see that we have an async block. 24 00:02:04,820 --> 00:02:12,590 Inside of our spawn, and it is necessary to have an async blank block in order to call async functions. 25 00:02:13,280 --> 00:02:19,130 And then just like in threads, we can call a blocking function to allow for the computation to finish. 26 00:02:19,610 --> 00:02:21,500 So let us do that. 27 00:02:21,500 --> 00:02:23,540 So we will say print line. 28 00:02:24,200 --> 00:02:31,470 Task has started and now we want to call our block on. 29 00:02:31,490 --> 00:02:35,090 So this is our blocking function on task. 30 00:02:36,830 --> 00:02:38,990 Which will make which will allow it to finish. 31 00:02:38,990 --> 00:02:42,560 And now we will say stopped task. 32 00:02:44,020 --> 00:02:50,800 And now if we run this, we accept we expect it to say task is started and then we expect the contents 33 00:02:50,800 --> 00:02:56,800 to be printed out and then we expect stoped task to be printed out after. 34 00:02:58,220 --> 00:03:04,160 And we see task has started file to read from which was the contents inside of our file and then we 35 00:03:04,160 --> 00:03:05,540 see stop task. 36 00:03:05,660 --> 00:03:10,670 So task are one of the core abstractions in async. 37 00:03:12,220 --> 00:03:17,620 Like threads, they provide some practical functionality over the raw concept. 38 00:03:18,420 --> 00:03:25,140 Tasks have a number of desirable properties such as being allocated in one single allocation. 39 00:03:26,250 --> 00:03:32,760 They have backchannel which allows them to propagate results and errors to the spawning task through 40 00:03:32,760 --> 00:03:33,780 the joint handle. 41 00:03:34,200 --> 00:03:40,170 They also carry useful metadata for debugging and they support task local storage. 42 00:03:40,830 --> 00:03:44,550 So all in all we can conclude that the task type. 43 00:03:45,630 --> 00:03:53,610 And our async library is very similar to a thread which is great for us since we are already familiar 44 00:03:53,610 --> 00:03:58,170 with threads and as you can tell, kind of by the structure of our program here. 45 00:03:59,060 --> 00:04:05,060 It has a very, very, very similar feel to what we've already seen in the concurrency section. 46 00:04:05,360 --> 00:04:12,500 So in order for us to really understand asynchronous programming and how it works and just get an overall 47 00:04:12,500 --> 00:04:18,200 better feel for it, let's create a client server chat application. 48 00:04:20,130 --> 00:04:21,750 Make it asynchronous. 49 00:04:21,750 --> 00:04:30,900 So that way we can practice using async code and rest and get a better understanding of how asynchronous 50 00:04:30,900 --> 00:04:32,550 programming works. 51 00:04:32,550 --> 00:04:36,030 And we will begin doing this in the next lecture.