When should cfthread be used?
Would you please give a simple scenario where threading is neces开发者_开发问答sary? Thank you, Nich
I used it in a situation where we needed to initiate some back end data processing to tabulate data prior to the user executing some reports. So after login we would fire off a task using cfthread to build the warehouse data for the user. Worked great!
So think of cfthread as a way to execute an asynchronous task on demand. Extremely useful in the right situation!
Use cfthread
if it makes sense to execute certain code in parallel with the main request processing code.
Example: say you are uploading a directory of files and you need to do the same processing to each, perhaps to save the contents into the database. You could then use cfthread
to run the processing on each file asynchronously. Consider this pseudocode:
<cfdirectory directory="x" action="read" name="allFiles" />
<cfloop query="allFiles">
<cfthread action="run" name="thread-#allFiles.name#>
<!--- Read your file in and do processing --->
</cfthread>
</cfloop>
Remember there is a setting CF Admin to set the number of threads you can spawn in this way! A lot will also depend on your system resources.
There are other examples around which describe other use cases in greater detail. Just do a Google search. I'd recommend Ben Nadal's cfthread primer as a good starting point.
One other warning: threads are a solution to all problems! I was using them to deal with queue processing before and ran into trouble.
In summary cfthread
is a great feature of ColdFusion, enjoy it!
I have used cfthread in two situations.
As noted above, I've used it when I wanted to do asynchronous processing while my main thread was doing other things. For example, I used it to load RSS data while generating the rest of the page. Since the call to the RSS source took a couple of seconds, I started a thread before beginning any other processing. It ran while I was querying, drawing the layout, etc. Then I joined the thread and displayed the RSS data. This prevented me from having to pause the page load while the RSS was populating.
The second way I've used this is sort of an on-demand alternative to using the scheduler. We were generating complex PDF documents. The person generating them didn't need them immediately, so rather than have the user stuck while we generated the document, we created an unjoined thread to process the PDF. We then restricted the number of cfthreads being processed at any ppoint. Now, regardless of load, the cfthreads would simply queue, and be processed as resources were available.
<!--- store value into message varaible --->
<cfset variables.message = "It's orginal value.">
<!--- create new thread --->
<cfthread name="ThreadOne">
<!--- overwrite new value into existing variable. --->
<cfset variables.message = "It comes from thread.">
</cfthread>
<!--- join thread --->
<!---
If we leave this join, the code within thread will execute but won't display the value.
--->
<cfthread action="join" name="ThreadOne" />
<!--- Output --->
<cfoutput>#variables.message#</cfoutput>
Check above coding. It's kinda clear.
精彩评论