Understanding Synchronous, Threading, and Async in Network Requests
Learn how to optimize network requests using synchronous, threading, and async methods. See practical examples and understand when to use each approach.
File
How to Make 2500 HTTP Requests in 2 Seconds with Async Await
Added on 10/01/2024
Speakers
add Add new speaker

Speaker 1: So when you run your code it will wait for each line to be executed or each block of code to execute before the next one can happen. Now if you put something in there that requires a bit of waiting time, like you're waiting on a server or a response from something external, your program's just going to be sat there doing nothing. Now there's a couple of ways around this, we can use threading and we can use async. I'm going to try and show you a few examples using a request to a server so I can show you how it all goes a bit quicker. So on the screen I have a synchronous version, this basically just goes out to this URL, sends a number 1 to 2,500 and then gives us a response back. Now as you can see when I ran this it took 5.4 seconds, so this is what I'm actually using as my demo. It is a server on the local host on my machine here on my network, so we're actually not getting an awful lot of delay in the actual response but that's just going to further compound so you can really see the difference in time taken and then you could extrapolate that out when you actually have to wait for a response back. We ignore this one, we're just hitting this endpoint which is just returning an ID, a number. So when I run this code here it's going to go through each line by line, when it gets the response back you can see it's printing it out to the screen here and it took 5.85 seconds, slightly slower than the time I ran it just a minute ago. So this is all well and good but what happens if we have thousands and thousands of URLs that we need to check on? This is going to take forever. So the first option is to use threading, so I'm going to be using here in this example we're using the Concurrent Futures, we're using the Thread Pool Executor which is basically going to allow us to use extra threads and it's going to speed it up ever so slightly. So generally speaking threads and multi-threading like this is better for if you're trying to work in parallel, so if your code is constantly doing something, you're not waiting for anything external, then this is probably going to be the best way for you. But if you're dealing with any network requests, then you are going to want to use async and I'll show you that in just a second. So let me just run this one quick, you can see it looks slightly different, maybe a bit quicker and this is going to come through 3.84 seconds, so slightly faster than that one there. This is fairly easy to use and fairly simple to set up, all you need is a function that does your thing for you and a list in this case because I'm using the map. There are different ways that you can use this, but this is how you would use it if you were making network requests. So the third option is async, now this is slightly more complicated because there is more to it. We need to have an extra function here so we can see that this is our main function that actually makes the request, then we have one here that creates all the tasks, we are using asyncIO here and it will await for the response for us. So this is basically like sending out all of your requests in one go sort of and it will manage and wait for each of the responses to come back. So it will basically remove any of that time spent waiting in your code. So I'm going to run this and we'll see that it will take literally about a second, 1.37 seconds, so it took 1.34 last time and we got all of the data back within a list. So when would you want to use any of these versions? Well if you're trying to request data from the same server over and over again you're going to find that you're going to get rate limited so you aren't generally going to be able to use this async version, but you can work it through because if for example the rate limit on your server that you're trying to get to is 300 calls per minute and you only need to make 200 calls, you can asynchronously send 200 and get all the responses back in a flash. Whereas if you're doing it synchronously you'd have to send 200 actual, 200 or 300 single requests which could take a couple of minutes. The other alternative is if you have lots of URLs that you need to visit for lots of different sites, lots of different servers, you can then use your async code to request them all in one go and get the responses back and then handle it that way. So there'll be a link to my github down below with these examples in and if you're interested in how you might use these in a different kind of way I think you're going to enjoy this video right here.

ai AI Insights
Summary

Generate a brief summary highlighting the main points of the transcript.

Generate
Title

Generate a concise and relevant title for the transcript based on the main themes and content discussed.

Generate
Keywords

Identify and highlight the key words or phrases most relevant to the content of the transcript.

Generate
Enter your query
Sentiments

Analyze the emotional tone of the transcript to determine whether the sentiment is positive, negative, or neutral.

Generate
Quizzes

Create interactive quizzes based on the content of the transcript to test comprehension or engage users.

Generate
{{ secondsToHumanTime(time) }}
Back
Forward
{{ Math.round(speed * 100) / 100 }}x
{{ secondsToHumanTime(duration) }}
close
New speaker
Add speaker
close
Edit speaker
Save changes
close
Share Transcript