Get value of variables running in cfloop using cfthread join
Asked Answered
L

3

5

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!!

Lett answered 14/7, 2010 at 16:51 Comment(0)
C
6

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>
Chokefull answered 15/7, 2010 at 18:38 Comment(1)
Good catches Anthony! I didn't look closely enough at the entire problem but luckily you really nailed it!Gestapo
B
3

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.

Basinet answered 14/7, 2010 at 17:11 Comment(1)
Hmmm, can you confirm that if you put <cfdump var=#objGet1# /> that it does dump that first one? Also, try setting the name in cfthread to "variables.objGet#intGet#" - shouldn't be necessary, but I've not needed to use cfthread yet, so not entirely sure how it behaves.Basinet
G
0

Code run inside a cfthread tag has its own scope. Try passing the variable you want it to access as an attribute. I like to name it something different just to help me keep track.

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

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

</cfthread>

Gestapo answered 15/7, 2010 at 18:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.