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 Output

00:00 So in the previous lesson, you introduced an iterable to your code, so all that’s left to do is to test it. So in the correct directory, type python async.py, and hit Enter and you will see very, very similar output, and leave it run until the end, which will take 10 seconds.

00:17 If everything goes well, it’ll say indeed, total time, 10 seconds. So let me scroll up to the top and you’ll see that the same thing is happening. Three tasks are started.

00:29 So 'Click on button' that is from laptop. This is from the coffee making, and this is from the opening of the parcels. And the rest of the code runs in exactly the same way.

00:41 The only thing you are not seeing is if you look back at the output from the iterator, there was extra output here. It would say that the iterator had started, which was print() functionality from the iterator’s __aiter__() method.

00:56 That code is still there, but that is no longer shown in the output because now there is an __aiter__() method from the iterable.

01:05 So now you see that with an iterable whose __aiter__() method, returns an iterator, you can achieve the exact same thing.

01:14 Now, a question you might ask is, well, why did you do that? Because actually you’ve just added more code and it looks to be a bit more convoluted because previously with just the iterator, we got the exact same output.

01:27 Indeed. That’s a fair comment. I mean, in practice, iterables usually use async generators, and that is explained in quite a bit of detail in the written tutorial, which I would recommend you have a look at.

01:41 I’ve just wanted to include this example here as a theoretical example of how you can use an iterable that returns the instance of an iterator class.

01:53 In the next lesson then, you’ll have a look at all the things you’ve learned and some potential next steps.

Become a Member to join the conversation.