CFTHREAD is Sweet!

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).

2 responses to “CFTHREAD is Sweet!”

  1. Kevin Sargent

    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..