Just had my first occasion to use CFTHREAD in an app, and it's nice. As part of rendering an image gallery, I wanted to ensure that the thumbnails (which are generated server-side) exist so that they the user doesn't have to wait for them to be auto-generated as they're requested. Unfortunately, generating the thumbnails is relatively slow, so I didn't want to do it as part of the page execution.
However, using CFTHREAD I can just put my thumbnail generation loop inside a "throwaway" thread that will execute in the background until it's finished, hopefully after the page goes back to the browser, but before the browser gets very far into requesting the thumbnails. That way most of the thumbnails should be pregenerated by the time they're requested.
This is nothing that you couldn't do with the Async CFML Event Gateway (or even a throwaway Ajax call), but the CFTHREAD method is much cleaner than either one. It also provides the capability to rejoin the threads with the main page thread if you need to do that, which is doubly nice, and something that you can't do with the async gateway (nor obviously an Ajax request, as that happens after the generated output is client-side).
would it not be much much better to only create thumbs once though, and not per request? ?
Even if they are uploaded via FTP, you can have a script check all new files and create thumbs & save some info in a db so you don't re-create thumbs all the time.
Heck, an CF gateway event could even watch the images folder, and on change (new file, file removed) could run a script to make the new thumbs, or update the db about the missing files, etc..
Kevin,
Yeah, that'd make sense for a more typical gallery application. In my case, things are a bit different in that there are tens (if not hundreds) of thousands of images, most of which aren't looked at in gallery format. Also factor in the fact that thumbnails are required at different sizes, some of them dynamic. As such, it's not really practical to pregenerate thumbnails.
I'm actually using mod_rewrite to do most of the lifting. Say I request a thumbnail such as "tn/150-150-img0000180687.jpg". mod_rewrite checks for that literal file on the filesystem, and if it exists, returns it to the browser. If it doesn't exist, it hands off to CF to generate the thumbnail, write it to disk, and serve it to the browser. On the next request that comes in, mod_rewrite will see the file already exists and serve it back directly.
That works amazingly well, but generating a large number of thumbnails can be expensive, so pregenerating specific subsets was the problem I was actual addressing.