Top Banner
Multithreading with <cfThread> Billy Cravens
22
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Cfthread Presentation

Multithreading with <cfThread>

Billy Cravens

Page 2: Cfthread Presentation

Who am I??? Billy Cravens Developing ColdFusion apps since version 4.0 Early involvement with .NET Consultant .. Hire me!!! [email protected] (713) 408-3052

Page 3: Cfthread Presentation

Enough about me… You didn’t come to hear my shameless self-

promotion

Page 4: Cfthread Presentation

Keys to success presentations Know your audience Make your presentation unique and fun Relevant, useful examples

Page 5: Cfthread Presentation

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

Page 6: Cfthread Presentation

Some basic examples Single service call: overhead + service time

Page 7: Cfthread Presentation

My simulated shipping service

Page 8: Cfthread Presentation

Call to single service

Page 9: Cfthread Presentation

Some basic examples Single service call: overhead + service time Multiple service calls: overhead + service 1

time + service 2 time + service N time …..

Page 10: Cfthread Presentation

Call to multiple services

Page 11: Cfthread Presentation

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

Page 12: Cfthread Presentation

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

Page 13: Cfthread Presentation

Run off and leave me alone <cfthread action=“run” name=“NameOfThread”>

<!--- whatever code you want goes here --->

</cfthread>

Page 14: Cfthread Presentation

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

Page 15: Cfthread Presentation

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

Page 16: Cfthread Presentation

Run thread and get results

Page 17: Cfthread Presentation

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!

Page 18: Cfthread Presentation

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?

Page 19: Cfthread Presentation

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

Page 20: Cfthread Presentation

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

Page 21: Cfthread Presentation

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”

Page 22: Cfthread Presentation

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