Lesson 13 of 18
Speed Things Up with Parallel Async Actions
In this lesson
Roy Osherove downloads several pages at the same time by wrapping the download actions in an async action group, which runs its child actions in multiple threads, with a maximum number of concurrent threads and an optional delay between thread starts. The group is also a sync point, as the action after it starts only when all of its actions finish, and he warns against async action groups for actions that change the same IO or write to the same variable, because of race conditions.
Transcript
[00:03] (external link, opens in new tab) Let's talk about a very powerful feature in FinalBuilder which allows speeding up multiple actions that take different amounts of time. And this is called async action groups. Let's say we have multiple pages we want to download at the same time, right? Let's fix the Google thing here so that it's actually working and let's download a few other sites. For example let's say red.com and
[00:48] (external link, opens in new tab) Now I could for example just to show how things work I could just rightclick here and say run selected actions and children just to see that everything is working one at a time. Now if I wanted to and if these things are not IO bound or not changing the same file etc. If we let them run async and if we have let's say a CPU with multiple cores or multiple CPUs we could run things in multiple threads. So one way to do it if we look for async here
[01:20] (external link, opens in new tab) We'll see an async action group or another way is to say select all these actions right click refactor wrap actions with async action group. Then we'll just move the action group left so it's not under the original group and remove the original group. Now if I try to run all these actions together, first of all, let's see what are the properties of the action group. Run all of the child actions asynchronously.
[01:53] (external link, opens in new tab) I can set the maximum concurrent threads. If I have two CPU cores, it will be two. I can even set thread start delay. So if the threads start together, maybe it could cause an an error. But I can make it even start each thread after each after 1 second. For now, let's just start all of them at the same time or it will run two at the same time. I'm going to right click and say run selected actions and children. And what you'll see is that two of them are running at the same time every time.
[02:26] (external link, opens in new tab) And the nice thing about action groups, async action groups, is that the action group is also a sync point. that is that the action after it it's sibling action not a child action will only start when the async action group all of them finish. So that's just simplified for us. If I try to run everything now should be able to see multiple threads running and only executing later.
[02:57] (external link, opens in new tab) And now we actually have an error. Sorry that's not an error. We're just exporting the logs. So, no errors happened. So, that's an example of async action groups, how how they can help speed things up. But beware, don't use async action groups on anything that changes the same IO because it might cause problems or if you're writing to the same variable in multiple actions, of course, could cause the same problem. Same idea of race conditions and all that stuff. So, that's something to be aware of.