Upload
billy-cravens
View
1.413
Download
0
Tags:
Embed Size (px)
Citation preview
Multithreading with <cfThread>
Billy Cravens
Who am I??? Billy Cravens Developing ColdFusion apps since version 4.0 Early involvement with .NET Consultant .. Hire me!!! [email protected] (713) 408-3052
Enough about me… You didn’t come to hear my shameless self-
promotion
Keys to success presentations Know your audience Make your presentation unique and fun Relevant, useful examples
The unthreaded web Request Server does its thing Response Components, functions, AJAX, web services,
flushing – still an HTTP request Wait til it’s done SOA: wait on them
Some basic examples Single service call: overhead + service time
My simulated shipping service
Call to single service
Some basic examples Single service call: overhead + service time Multiple service calls: overhead + service 1
time + service 2 time + service N time …..
Call to multiple services
Some basic examples Single service call: overhead + service time Multiple service calls: overhead + service 1
time + service 2 time + service N time ….. Each service is synchronous – wait til previous
one finished
Types of threading in web apps For our purposes, 2 types:
Send a task away and leave us alone Perform multiple tasks at the same time and wait
for results
Run off and leave me alone <cfthread action=“run” name=“NameOfThread”>
<!--- whatever code you want goes here --->
</cfthread>
Limitations of CFThread Doesn’t support concept of call-backs
Either the thread runs and is done when its done with no notification anywhere or
Parent thread must wait for all child threads to finish and take action then
Parallel threads in a page Basic example: a single thread in a page
When we just call a thread without joining it to page level thread, it shoots off into never never land
So to use results of thread in page: At least one thread to execute Join our threads together
Thread results available via threadName or cfthread structure
Run thread and get results
Caveat (Limitation?) No output! Even if you put output tags Can access output (and other neat metadata)
by cfdump’ing thread scope Problem: CFDump will strip out HTML
Solution: just output threadName.output Especially if you want to CFDump inside your
thread!
Multiple threads Same process, just keep track of your thread
names Pass as a list when joining Loop over cfthread structure to access
contents of each Remember scope:
Variables set inside of thread do not affect page-level
Remember to be “thread safe”! Unpredictable results when you attempt to read variables not local to thread
Use attributes scope (remember custom tags?) No control over order data returned Big question: performance enhanced?
Watch out! Other variables: can always write over the
variable Threads: must always be named unique!!!!
Make unique (using CreateUUID() to be safe) But… CFThread scope continues for life of
request Loop over list of thread names instead
Other limitations No grandparents: can only have one level of
child threads Each thread sucks up a request ?? Adobe CF: Standard limited to 10 threads
(regardless value set in CF Admin) – rest are queued Must buy Enterprise BlueDragon and Railo other limits
Not part of application error handling Error: look at threadScope.error structure
Favorite tricks: Timeout a code block Specify timeout to your page level thread Continues when timeout expires Look at status to determine if child thread
completed (threadName.status) Doesn’t kill child thread! Only proceeds without it Kill threads in CF Admin or via action=“terminate”
Conclusion Synchronous code forces us to wait Asynchronous code lets us take advantage of
more resources, enhancing performance Must rethink how data is passed around