6 Replies Latest reply on Feb 28, 2010 11:37 AM by Adam Cameron.

    Using Cfthread to separate intensive task from page processing

    Aquitaine Level 1

      Hello there,

       

      I have what I think is a fairly simple need to separate out a fairly intensive bit of code from the regular page processing. This is under CF8.

       

      We have a page that does a lot of survey result analysis on tens of thousands of records. It's used very rarely and only by one or two people, but our server isn't really equipped to do that kind of thing very quickly.

       

      The consequence is that the entire number-crunching process can push the page load to about 120 seconds. This is fine since everyone involved knows what's going on and why, but I'd display a 'Please wait' sort of message and load all of the layout so it's not just 'waiting for my.web.site...' in their browser.

       

      I put the number crunching section of code into a cfthread and that seemed to do what I want, at last in terms of loading the rest of the page first. The problem is that the thread never outputs anything - nothing that is processed in the thread gets displayed on the page.

       

      This is the first time I've used CFTHREAD, and I understand it conceptually from a data processing perspective (such as the examples given in the documentation when you're performing asynchronous tasks like downloading something or grabbing an external RSS feed). I'm just not sure how it relates to page processing. In other words, it seems to me that I'm either doing something simple incorrectly, or else CFTHREAD can't be used this way.

       

      Thanks very much,

      Aq

        • 1. Re: Using Cfthread to separate intensive task from page processing
          BKBK Adobe Community Professional & MVP

          You should control thread flow, for example, like this:

           

          testpage.cfm

          ============

           

          <!--- page display, including 'please wait, this might take a minute or two' message to user --->

           

          <cfthread action="run" name="heavyDuty">
              <!--- heavy-duty calculations --->
          </cfthread>

           

          <!--- the main thread waits 2 minutes (120s) for the heavy-duty thread to finish --->

          <!--- (choose time interval long enough for calculations to complete) --->
          <cfthread action="join" name="heavyDuty" timeout="120" />

           

          <!--- code that displays result of the heavy-duty calculation --->

          • 2. Re: Using Cfthread to separate intensive task from page processing
            Adam Cameron. Level 5

            I don't think <cfthread> is really relevant to your situation here.  If you wanted to "fire and forget" the process, and didn't care about any post-execution feedback, it'd be perfect.

             

            But given you do want feedback, just run it within the main thread, but out a <cfflush interval="#someValue#"> at beginning of the process.

             

            --

            Adam

            • 3. Re: Using Cfthread to separate intensive task from page processing
              Adam Cameron. Level 5

              Why would one even use <cfthread> here if you're just going to make the main thread hang around and wait for it to finish anyhow?

               

              Seems like you're just using two threads here when one would do??

               

              Am I missing something?

               

              --

              Adam

              • 4. Re: Using Cfthread to separate intensive task from page processing
                BKBK Adobe Community Professional & MVP

                A Cameron wrote:

                 

                I don't think <cfthread> is really relevant to your situation here.  If you wanted to "fire and forget" the process, and didn't care about any post-execution feedback, it'd be perfect.

                 

                But given you do want feedback, just run it within the main thread, but out a <cfflush interval="#someValue#"> at beginning of the process.

                What my example models isn't exactly fire-and-forget. It is fire-and-forget-and-remind. To borrow your word, the main thread waits for feedback from the heavyDuty thread.

                • 5. Re: Using Cfthread to separate intensive task from page processing
                  BKBK Adobe Community Professional & MVP

                  Forgive my taking the liberty to rephrase your question. I hope you will agree it makes my answer brief and sharp.

                   

                   

                  Am I missing something?

                   

                  Seems like you're just using two threads here when one would do??

                   

                  Why would one even use <cfthread> here if you're just going to make the main thread hang around and wait for it to finish anyhow?

                   

                  Control. The main thread delegates, and could itself do some useful work while waiting. ( A loose comparison is the continuous stream of information you get while waiting for the Windows Coldfusion installer to finish).

                   

                  Anyway, we have to answer Aquitaine's question, not ask him one. His question is about "Using Cfthread to separate intensive task from page processing".

                  • 6. Re: Using Cfthread to separate intensive task from page processing
                    Adam Cameron. Level 5

                    Fair enough: it wasn't clear (to me) that that was what you were trying to articulate with your code example.  But on re-read I can

                    kinda see that you might have been.

                     

                    --

                    Adam