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

Coding the Coroutine

00:00 So in the previous lesson, you built the task iterator and you added the subtask lists. In this lesson, you will add some code to be able to run those task iterators in parallel, so using async programming. The first thing to do is to introduce a coroutine called async def run_iterator.

00:23 And that coroutine just runs the iterator as the name suggests. Now there’s quite a bit of print functionality in there, so I’m just going to copy-paste that to save yourself some time.

00:35 So there’s an async def on line 35 called run_iterator, and the input parameter is an iterator. Line 36 then just runs an async for loop for each subtask in that iterator.

00:50 That’s all that happens then. Line 37, 38, and 39, that’s just a print functionality in your terminal to show you when you look at the output exactly what is happening.

01:02 And the final thing to do then is scroll down to the bottom,

01:08 and you can choose to delete the task list if you want, that’s no longer used. So just clean things up a bit. I’ll delete that. But if you look at now line 54 and line 55, as you will remember from the example, that is where the multitasking happens.

01:25 So using gather from asyncio. Currently, this is looking at the task list, which I just deleted. So instead of running a task, I will now want to run an iterator.

01:38 So instead of run_task, I’m going to use the coroutine that I just created. So run_iterator. First scroll up a little bit. run_iterator on line 35, takes an iterator as an input parameter.

01:54 So which iterator am I going to stick in there? Well, that will be an instance of my TaskIterator. TaskIterator, that’s my class.

02:05 And to instantiate the class, I needed an iterator name and I needed a task list to go in there. The name of my first iterator, I’m going to call that "Laptop Tasks", and the list to go in there, those are the subtasks of the laptop tasks.

02:23 So that was called LAPTOP_TASKS all in capital.

02:28 Remember that is one of the lists that I copied in at the very start. I’m just going to clean this up a little bit and then do some copy-pasting because I want to do the same thing for the other tasks.

02:43 So I am going to run the iterator again. This time, it’s going to be a different instance of the TaskIterator, this going to be called "Coffee Tasks".

02:51 And the name of that list is COFFEE_TASKS.

02:57 And then my third task was to open a parcel. So for that task, I’m also going to create a separate iterator. So a new instance of the task iterator on line 57.

03:08 And that then will be called "Parcel Tasks".

03:12 And the list with the subtasks of the parcel tasks was called PARCEL_TASKS.

03:19 There you have it. So what has changed now is that gather, remember gather is the functionality that runs tasks asynchronously.

03:29 So now I’m running in parallel, or in async mode if you like, three iterators. So line 55 is my first iterator, which will be the laptop tasks that is the iterator that iterates over the subtasks that are needed to perform the laptop startup task.

03:48 On line 56, there’s another instance that is the iterator that iterates over all the coffee making subtasks. And then on line 57, that is the iterator that iterates over all the parcel opening subtasks.

04:04 And because you are now using gather, there will be multitasking between those three iterators.

04:11 So all that’s left to do now is to test the code, and you will do that in the next lesson.

Become a Member to join the conversation.