際際滷

際際滷Share a Scribd company logo
Multithreading with <cfThread>Billy Cravens
Who am I???Billy CravensDeveloping ColdFusion apps since version 4.0Early involvement with .NETConsultant .. Hire me!!!billy@billycravens.com(713) 408-3052
Enough about meYou didnt come to hear my shameless self-promotion
Keys to success presentationsKnow your audienceMake your presentation unique and funRelevant, useful examples
The unthreaded webRequest Server does its thing ResponseComponents, functions, AJAX, web services, flushing  still an HTTP request Wait til its doneSOA: wait on them
Some basic examplesSingle service call: overhead + service time
My simulated shipping service
Call to single service
Some basic examplesSingle service call: overhead + service timeMultiple service calls: overhead + service 1 time + service 2 time + service N time ..
Call to multiple services
Some basic examplesSingle service call: overhead + service timeMultiple 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 appsFor our purposes, 2 types:Send a task away and leave us alonePerform 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 CFThreadDoesnt support concept of call-backsEither the thread runs and is done when its done with no notification anywhere orParent thread must wait for all child threads to finish and take action then
Parallel threads in a pageBasic example: a single thread in a pageWhen we just call a thread without joining it to page level thread, it shoots off into never never landSo to use results of thread in page:At least one thread to executeJoin our threads togetherThread results available via threadName or cfthread structure
Run thread and get results
Caveat (Limitation?)No output! Even if you put output tagsCan access output (and other neat metadata) by cfdumping thread scopeProblem: CFDump will strip out HTMLSolution: just output threadName.outputEspecially if you want to CFDump inside your thread!
Multiple threadsSame process, just keep track of your thread namesPass as a list when joiningLoop over cfthread structure to access contents of eachRemember scope:Variables set inside of thread do not affect page-levelRemember to be thread safe! Unpredictable results when you attempt to read variables not local to threadUse attributes scope (remember custom tags?)No control over order data returnedBig question: performance enhanced?
Watch out!Other variables: can always write over the variableThreads: must always be named unique!!!!Make unique (using CreateUUID() to be safe)But CFThread scope continues for life of requestLoop over list of thread names instead
Other limitationsNo grandparents: can only have one level of child threadsEach thread sucks up a request ??Adobe CF: Standard limited to 10 threads (regardless value set in CF Admin)  rest are queuedMust buy EnterpriseBlueDragon and Railo other limitsNot part of application error handlingError: look at threadScope.error structure
Favorite tricks: Timeout a code blockSpecify timeout to your page level threadContinues when timeout expiresLook at status to determine if child thread completed (threadName.status)Doesnt kill child thread! Only proceeds without itKill threads in CF Admin or via action=terminate
ConclusionSynchronous code forces us to waitAsynchronous code lets us take advantage of more resources, enhancing performanceMust rethink how data is passed around

More Related Content

Cfthread Presentation

  • 2. Who am I???Billy CravensDeveloping ColdFusion apps since version 4.0Early involvement with .NETConsultant .. Hire me!!!billy@billycravens.com(713) 408-3052
  • 3. Enough about meYou didnt come to hear my shameless self-promotion
  • 4. Keys to success presentationsKnow your audienceMake your presentation unique and funRelevant, useful examples
  • 5. The unthreaded webRequest Server does its thing ResponseComponents, functions, AJAX, web services, flushing still an HTTP request Wait til its doneSOA: wait on them
  • 6. Some basic examplesSingle service call: overhead + service time
  • 8. Call to single service
  • 9. Some basic examplesSingle service call: overhead + service timeMultiple service calls: overhead + service 1 time + service 2 time + service N time ..
  • 10. Call to multiple services
  • 11. Some basic examplesSingle service call: overhead + service timeMultiple service calls: overhead + service 1 time + service 2 time + service N time ..Each service is synchronous wait til previous one finished
  • 12. Types of threading in web appsFor our purposes, 2 types:Send a task away and leave us alonePerform multiple tasks at the same time and wait for results
  • 13. Run off and leave me alone<cfthread action=run name=NameOfThread> <!--- whatever code you want goes here ---></cfthread>
  • 14. Limitations of CFThreadDoesnt support concept of call-backsEither the thread runs and is done when its done with no notification anywhere orParent thread must wait for all child threads to finish and take action then
  • 15. Parallel threads in a pageBasic example: a single thread in a pageWhen we just call a thread without joining it to page level thread, it shoots off into never never landSo to use results of thread in page:At least one thread to executeJoin our threads togetherThread results available via threadName or cfthread structure
  • 16. Run thread and get results
  • 17. Caveat (Limitation?)No output! Even if you put output tagsCan access output (and other neat metadata) by cfdumping thread scopeProblem: CFDump will strip out HTMLSolution: just output threadName.outputEspecially if you want to CFDump inside your thread!
  • 18. Multiple threadsSame process, just keep track of your thread namesPass as a list when joiningLoop over cfthread structure to access contents of eachRemember scope:Variables set inside of thread do not affect page-levelRemember to be thread safe! Unpredictable results when you attempt to read variables not local to threadUse attributes scope (remember custom tags?)No control over order data returnedBig question: performance enhanced?
  • 19. Watch out!Other variables: can always write over the variableThreads: must always be named unique!!!!Make unique (using CreateUUID() to be safe)But CFThread scope continues for life of requestLoop over list of thread names instead
  • 20. Other limitationsNo grandparents: can only have one level of child threadsEach thread sucks up a request ??Adobe CF: Standard limited to 10 threads (regardless value set in CF Admin) rest are queuedMust buy EnterpriseBlueDragon and Railo other limitsNot part of application error handlingError: look at threadScope.error structure
  • 21. Favorite tricks: Timeout a code blockSpecify timeout to your page level threadContinues when timeout expiresLook at status to determine if child thread completed (threadName.status)Doesnt kill child thread! Only proceeds without itKill threads in CF Admin or via action=terminate
  • 22. ConclusionSynchronous code forces us to waitAsynchronous code lets us take advantage of more resources, enhancing performanceMust rethink how data is passed around