Coding an Iterator Example - __anext__
00:00
In the previous lesson, you started coding the TaskIterator class. You coded the __aiter__ special method. The next one is __anext__.
00:09
Now this needs to be an async def method. So __anext__, and that also takes self as an input parameter. And okay, have another look at the slides and see what the requirement is for __anext__.
00:28
So __anext__ must return the next element of the async iterable, okay? It must return an awaitable object and it must raise a StopAsyncIteration exception when the data stream is exhausted.
00:42
Okay, so what do you do in your code? Well, firstly, let’s deal with the StopAsyncIteration. So when the data is exhausted, what that means is when we’ve reached the end.
00:53 So when the iterations reach five, as per our input parameter on line 16, or if the counter reaches the length of our task list. So in other words, if the subtasks all have been done, then we want our iterator to stop.
01:10
So to code that, we’ll just code those two conditions. So if the counter is bigger than the end, so if self.counter is larger or equal than self.end, or if self.counter is equal to the length of our task list, self.task_list.
01:34
So if that’s the case, then you need to raise the StopAsyncIteration exception.
01:43
So that’s that. Secondly, I will want to run my subtasks. Now for the subtasks to run, we had a generic run_task() function, which takes the subtask name and the duration as input parameters.
01:57 So we need to extract those first. So I’m going to extract those from the task lists.
02:07
Remember, the task lists have tuples, and those tuples have the elements that I need. So subtask name and duration. So I just use counter to pick up the correct tuple within the task list.
02:19
So then let’s run our subtask. Our subtask is await run_task(),
02:27 and then you feed in the subtask name and the duration.
02:33 So that’s the task for multitasking. You need to increase the counter by one
02:42 and then return the subtask.
02:47
Now a quick comment here where there seems to be quite some confusion or definitely I was confused to start with. So the __anext__ method has to return an awaitable object.
02:57 Now, the way I interpret that was that on line 32, it had to literally return an awaitable object, which is what it’s doing right now. So on line 30, we create the awaitable object, and on line 32, we return it.
03:12
Now, this is not to be interpreted as literally. The fact that you are using async def and the fact that you use await, that in itself is enough to return an awaitable object.
03:25
So you will see examples, for example, where there is no such thing as the subtask, that’s just the await keyword. And then what is returned is self.counter.
03:37 So you will see examples of that in the written tutorial, but I prefer to do it this way, and both ways work. So you have created your task iterator, and you have introduced your subtask lists.
03:51 In the next lesson, you will add some more code to actually run the iterator and to then multitask across the iterators.
Become a Member to join the conversation.
