Multiple Threads in ColdFusion

I have gotten a lot of great feedback and suggestions from yesterday’s post about new ColdFusion features. One that really interests me is the idea of a CFFORK or a CFTHREAD tag. I agree that certain operations would benefit from the ability to happen concurrently (in fact, keep your eye out for DRK 5!), but I’m wondering what specifically people envision spawning new threads for. And what would the programming model be? When you spawn a new thread, execution obviously continues in the original thread, so how do you know when the new thread is finished, or get back the results of whatever operation it performed, or know if an error occurred. One way to do it is with callback functions, which may be something that can be built into components. I have an interesting idea of how this can be done (again, keep an eye out for DRK 5), but I want to get some other suggestions, as well.

19 Responses to Multiple Threads in ColdFusion

  1. Shawn Porter says:

    I was thinking that when you cffork you can give it a template name and a lock name. The lock name would create an exclusive named lock with that name that releases when the thread is finished. The template would be called like a cfmodule tag would except w/o access to caller scope. If you want to wait for the spawned thread later in your code you can do a cflock using the appropriate name. Perhaps a cfkillfork tag or something like that would make it possible to terminate a spawned thread from your primary thread.Handling errors in spawned threads is tricky. I’d guess that they should just terminate the whole request and throw the error. But that doesn’t really cover everything because what if you want to handle the error in your main thread. Wow, this could get really wierd.As far as getting the results of the operation in the spawned thread, they could be put in request scope (so cflock would need to be extended to allow locking of request scope) or they may be database, file, or cfhttp operations where there is no need for their results to be noticed in the primary thread unless there is an error.I think it is fine if writing multi threaded CF code is really tricky and akward. It is one of those features that wouldn’t come in handy very often and would probably be a strange case anyways. But it would be nice to have it as an option.I recall a few times in the last several months where I’ve thought to myself that a cffork tag would be useful but I can’t remember specifically why I wanted it. When it comes to me I’ll post it here.

  2. I have wished for native ColdFusion mutithreading for years. One of many instances where it would have been useful for is when I was synchronizing two disparent databases through webservices (not the best method, I know, but the only solution available for the client). The vendor only allowed access to one record per request through the webservice API. So I looped over the number of records calling the webservice each time, building an array upon which I then wrote off the array into our database.The inital looping solution took 10 minutes to complete with circa 2000 records.A multithreaded solution was eventually written where application scoped java object was used to spawn a thread for each record that used SOAP to get the data via the webservice and add that data to a synchronized data structure (ArrayList).The multithreaded solution took just 30 seconds.Brandon Smith

  3. Matt Knight says:

    I came across a need for multithreading….A page that has a CFSSTOREDPROC call, but you don’t want to wait for the database response, instead, create a new thread that calls the SP, then move on in the template and not wait for the thread to complete.

  4. Brian LeRoux says:

    I think its a solution looking for problems. CFML is not a language for multithreaded programming.If you guys concentrated on bringing ECMA-262 strict to CFSCRIPT however…. 😉

  5. Rob Brooks-Bilson says:

    I definitely have a problem that calls for multi-threading – asynchronous messaging.

  6. So for syntax, something like this?:cfthread action=”(sleep|kill|new)” [process=”(function or template to run)”] [time=”ms for sleeping”]Any/everyone else pls make suggestions.And what would we use it for? I second the calling a procedure that you don’t care about results (if any) are returned. Calling an email template, errors on that template can be ignored by my thread. having a process on a thread kill could send information to the administrator. thread sleeping while we wait for another thread or external process to catch up (although that can currently be accessed through java.lang.Thread.sleep()).

  7. Rob Brooks-Bilson says:

    Nathan,I’d like to see the syntax more of a wrapper, like this:cffork attributescode or include to code/cffork

  8. Marc Wallace says:

    I second the comment that this isn’t really what CFML is for.With MX you can already use JSP, call up to Java code, etc. It can do your threading.There’s a tendancy in the last decade or so to make every language incorporate every semantic feature that any other language has. Like Microsoft’s changes to VB — did we really need another Javaesque language?Let CFML remain a (primarily) scripting language.

  9. Stacy Young says:

    Maybe this is too simplistic but I envision it like:do somethinfdo somethingCF processing would halt until all threads of the multithread tag have completed execution (or timed out).While it’s not THAT often I would use this…it would be VERY handy when constructing reporting applications that operate against mutiple datasources. i.e. kick off 3 queries at the same time rather than process sequentially.My suggestion, keep it simple initially.

  10. Stacy Young says:

    Maybe this is too simplistic but I envision it like:[ cfmultithread ][cfthread id=”1″ timeout=”10″]do somethinf[cfthread][cfthread id=”2″]do something[cfthread][/cfmultithread]CF processing would halt until all threads of the multithread tag have completed execution (or timed out).While it’s not THAT often I would use this…it would be VERY handy when constructing reporting applications that operate against mutiple datasources. i.e. kick off 3 queries at the same time rather than process sequentially.My suggestion, keep it simple initially.

  11. Dennis Spaag says:

    Another CF blogger – Pete Freitag – wrote about a related tag idea: CFYIELD. A tag that basically says, “I can wait.”http://cfm.blogspot.com/2003_08_01_cfm_archive.html#106204965192611028

  12. Buy.com says:

    Handling errors in spawned threads is tricky. I’d guess that they should just terminate the whole request and throw the error. But that doesn’t really cover everything because what if you want to handle the error in your main thread. Wow, this could get really wierd.

  13. CPayne says:

    For those long-running async tasks that you don’t want to wait 4, use cfhttp with timeout=1 and throwonerror=no. Been doing it for years.

  14. ike says:

    The comment from Rob about asynchronous messaging requires more context to be very useful.Matt: I already have a solution for calling a stored procedure without delaying content delivery to the user. I execute the procedure in the OnRequestEnd.cfm template after a cfflush tag which allows me to perform long-running processes on a page that still produces blinding fast content delivery. Though in my case it’s not just for stored procedures — it’s for anything that takes time and that doesn’t need to provide content to the visitor. It’s all explained on the Event Model page in the Advanced Concepts section of the onTap framework docs.

  15. Isaac Dealey says:

    I like Stacy’s suggestion of sub-tag syntax. This would provide both simple syntax (I would drop the timeout attribute), as well as providing a solution to error handling ambiguity. The template could gather any errors from each thread and display all errors and terminate the request at the end of the parent tag. The thread ID could be optional for idenfitication purposes (added to the error message), and cfloop could be used to spawn multiple threads with the same purpose.

  16. gabe says:

    did anyone make something like this? i have a need for it as well to spawn a bunch of http gets.gabe

  17. bcarr says:

    I realize that this is an older post – but I’ve written a cfmultithread package (a small collection of .cfc’s strictly for use with CFMX 7.0) that allows concurrent CF execution on multiple threads while at the same synchronizing the return (results) of all executions. I’d be happy to send it to anyone who may be interested in it – email me at bcarr@insight.com.

  18. bcarr says:

    or bcarr14@hotmail.com (just in case spam filters kill your email)

  19. RikiKiki says:

    You’ve made my day! It’s very nice!