How to wait until a file is downloaded or why cfthread is a bad idea within a cfloop
I just run into an issue where one of our servers was totally bogged down when users started to download 100 or more files. See, the problem is that we had to check that each file was downloaded successfully before processing with the rest of the code. In other words;
1) Query for files that need to be downloaded
2) Loop over the records
3) Within loop download the file
4) check if file has been downloaded successfully
5) continue with the code.
In Coldfusion (or CFML to be general) your code would look like:
As I mentioned above, we need to check if the file has been downloaded successfully before continuing with the next download. That’s why there is a cfthread in this code. Now, while this might be a good idea with small download, it has a hazardous effect if you will trigger 1000 downloads, which again would create 1000 threads!!!
So, there are actually 2 things I’ve learned from this:
- There is no need for the cfthread. Since cfhttp will halt the code process until the file has been downloaded, creating the thread is not needed.
- Creating a thread within the iteration of your resultset (cfloop) is a VERY bad idea!