Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get value of variables running in cfloop using cfthread join

Thanks for replying!! But I am still not able to do it. Error that I am getting is "Element objGet1 is undefined in a Java object of type class coldfusion.runtime.VariableScope."

Below is my full code. I just want to dump the value of each thread containing cfhttp information.

http://www.google.com/search?" & "q=Vin+Diesel" & "&num=10" & "&start=") />

<cfset intStartTime = GetTickCount() />

<cfloop index="intGet" from="1" to="10" step="1">

    <!--- Start a new thread for this CFHttp call. --->
    <cfthread action="run" name="objGet#intGet#">

        <cfhttp method="GET" url="#strBaseURL##((intGet - 1) * 10)#" useragent="#CGI.http_user_agent#" result="THREAD.Get#intGet#" />

    </cfthread>

</cfloop>

<cfloop index="intGet" from="1" to="10" step="1">

    <cfthread action="join" name="objGet#intGet#" />
    <cfdump var="#Variables['objGet'&intGet]#"><br />

</cfloop>

and when I use after thread joining inside the loop. I get the desired results Thanks!!

like image 631
Deepak Yadav Avatar asked Jul 14 '10 16:07

Deepak Yadav


2 Answers

Two problems happening here.

As pointed out by Zugwalt, you need to explicitly pass in variables that you want to reference within the scope of your thread. He missed the CGI variable, that scope doesn't exist within your thread. So we pass in just what we need to use in the thread, userAgent, strBaseURL, and intGet.

Second problem, once joined, your threads are not in variable scope, they are in the cfthread scope, so we have to read them from there.

Corrected code:

<cfloop index="intGet" from="1" to="2" step="1">

    <!--- Start a new thread for this CFHttp call. Pass in user Agent, strBaseURL, and intGet --->
    <cfthread action="run" name="objGet#intGet#" userAgent="#cgi.http_user_agent#" intGet="#intGet#" strBaseURL="#strBaseURL#">

        <!--- Store the http request into the thread scope, so it will be visible after joining--->
        <cfhttp method="GET" url="#strBaseURL & ((intGet - 1) * 10)#" userAgent="#userAgent#" result="thread.get#intGet#"  />

    </cfthread>

</cfloop>

<cfloop index="intGet" from="1" to="2" step="1">

    <!--- Join each thread ---> 
    <cfthread action="join" name="objGet#intGet#" />
    <!--- Dump each named thread from the cfthread scope --->
    <cfdump var="#cfthread['objGet#intGet#']#" />

</cfloop>
like image 183
Anthony Avatar answered Oct 18 '22 07:10

Anthony


Generally, unscoped variables get put into the Variables scope, so you can use struct bracket notation to refer to them:

Variables['objGet#intGet#']

or

Variables['objGet'&intGet]

These are both basically doing the same thing - just different syntaxes.

like image 31
Peter Boughton Avatar answered Oct 18 '22 07:10

Peter Boughton