Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Testing the Iterator Example

00:00 To test the code, make sure you go to the directory where async.py is saved, and then type python async.py, and hit Enter. Now there will be quite a bit of output and I will need to scroll back up to the top, but this is what you will get.

00:14 You can see tasks are started and things are finished, and it will take a while. And after 10 seconds, there will be a message that all the tasks have been performed in 10 seconds, as you can see at the very bottom, total time, 10 seconds.

00:29 So I’ll scroll up to the very start

00:32 and you can see that firstly it starts the three iterators at the same time. So Iterator Laptop Tasks has started. That’s the iterator. And then the first subtask Click on button and wait is then started.

00:47 And the same thing happens to the coffee iterator where the first task is started, and the parcel iterator where the first subtask is started as well. Now it turns out that this getting scissors only takes a minute.

01:01 So that’s finished first and then says, okay, finished is the subtask 'Get scissors' from the iterator Parcel Tasks. So let’s iterate over that iterator Parcel Tasks, and therefore let’s start the second subtask 'Cut packaging'.

01:20 Now, in the meantime, there’s another task that’s finished that is 'Click on button, then wait', which took two seconds or two minutes in our analogy.

01:29 And so that is a task from the laptop iterator. So next line says that indeed subtask 'Click on button' and then wait from the 'Laptop Tasks' iterator is finished.

01:42 So let’s iterate over that iterator and therefore start the next task in line, which is entering the password. By the time you start that, there is a task from another iterator that is finished and that is boiling the water here from the coffee tasks.

01:59 So indeed it says that the subtask boil water from the iterator 'Coffee Tasks' has finished, and therefore let’s iterate over 'Coffee Tasks'.

02:09 Therefore, start the next task in the 'Coffee Tasks' subtask list. And that is adding coffee, milk, and sugar, and then letting it brew for a minute.

02:19 By now, I guess you get the idea. So I invite you to go through the output yourself and check that firstly, all tasks have been performed, and secondly that they all have been performed in the correct order.

02:34 Also, note that the whole thing took 10 seconds or 10 minutes in our analogy as we expected. So multitasking did happen. But if you go through the list here, you will find that the subtasks within each iterator have been performed in the right order.

02:53 So all the subtasks for laptop tasks, for example, were performed in the right order. So first you click on the on button, and only then will you enter your password.

03:03 And the same thing applies to the other two iterators. So this is what we were saying when you were looking at slides, is that the tasks will be performed in a fixed order within each iterator, but there is multitasking between the three iterators.

03:21 There you have it, that is your iterator. In the next lesson, you will now create an iterable. So an async iterable based on the async iterator that you have just built.

Become a Member to join the conversation.