CFTHREAD/CFJOIN Proof of Concept

Damon Cooper of Adobe has posted a proof of concept for two new tags for the ColdFusion language: CFTHREAD and CFJOIN. These tags let you fire off events and simply forget about them. Right now there is an issue where you can’t “completely” forget about them, but you can still run a large set of events asynchronously. Consider this example: <cfif not isDefined("url.slow")> <cfset good = true> <cfelse> <cfset good = false> </cfif>

<cfhttp url=”http://ray.camdenfamily.com/rss.cfm” result=”result”> <cfset myrss = result.filecontent> <cfset myrssParsed = xmlParse(myrss)> <cfset myurls = xmlSearch(myrssParsed, “/rss/channel/item/link/text()”)> <cfset links = arrayNew(1)>

<cfloop index=”x” from=”1” to=”#arrayLen(myurls)#”> <cfset arrayAppend(links, myurls[x].xmlvalue)> </cfloop>

<cfif good>

&lt;cfloop index="loopcounter" from="1" to="#arrayLen(links)#"&gt; 
	&lt;cfset threadname = "thread_" & loopcounter&gt;

	&lt;cfthread name="#threadname#"&gt;
		
		&lt;cfhttp url="#links[loopcounter]#" result="result"&gt;
		&lt;cffile action="write" file="c:\web\url#loopcounter#.txt" output="#result.filecontent#"&gt;
		
	&lt;/cfthread&gt;

&lt;/cfloop&gt;

&lt;cfloop index="loopcounter" from="1" to="#arrayLen(links)#"&gt; 
	&lt;cfset threadname = "thread_" & loopcounter&gt;
	&lt;cfjoin thread="#threadname#"&gt;
&lt;/cfloop&gt;

<cfelse>

&lt;cfloop index="loopcounter" from="1" to="#arrayLen(links)#"&gt; 
		&lt;cfhttp url="#links[loopcounter]#" result="result"&gt;
		&lt;cffile action="write" file="c:\web\url#loopcounter#.txt" output="#result.filecontent#"&gt;
		

&lt;/cfloop&gt;

</cfif>

<br> Work Complete!<br> </code>

What I’ve done is written two sets of code. One shows the use of cfthread/cfjoin, and one does not. The code downloads my RSS feed and gets an array of URLs. It then fetches each URL and saves it to a file. If you compare the “good” version versus the “bad” one (load the file with slow=true in the URL), you will see an incredible speed difference, and this is even with the bug. While the threads all run at the same time, you have to wait for the threads to end, whereas in the (hopefully) final version, you would not need to.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support.

Lafayette, LA https://www.raymondcamden.com

Comments