2

I've not used cfthread before but i'm using the following code and it's not working.

<cfloop from="1" to="5" index="local.pageNo">

            <cfthread name="thr#local.pageNo#" threadIndex="#local.pageNo#" action="run">

                <cfif local.pageNo GT 1>
                    <cfhttp url="#local.apiURL#&page=#local.pageNo#" method="get" result="local.myResults" >
                    </cfhttp>
                    <cfset local.myResponse = deserializejson(local.myResults.filecontent)>
                </cfif>

                <cfloop from="1" to="#arrayLen(local.myResponse.result)#" index="i">
                    <cfset local.apartmentList = listAppend(local.apartmentList,local.myResponse.result[i].id & '-0')>
                </cfloop>

            </cfthread>

        </cfloop>

        <cfthread action="join" name="thr1,thr2,thr3,thr4,thr5"/>

I'm expecting local.apartmentList to be a big list of ID's but it returns empty. It's almost as if the code inside the thread is just being skipped. Can anybody spot what i'm doing wrong?

James Privett
  • 1,079
  • 3
  • 15
  • 23
  • 1
    I would expect this code to fail; you loop 1..5 but you only create `local.myResponse` from the 2nd iteration onwards. Yet you refer to it later as if it always exists – duncan Jul 18 '14 at 09:45
  • Can I recommend you look @ your application & exception logs. They will have your errors in them. – Adam Cameron Jul 18 '14 at 09:47

1 Answers1

5

When you're using <cfthread>, the code within those tags is not run within the same context as the code around it. So you need to pass any variables you intend to use into it (as attributes of the <cfthread> tag), or "share" them via the request scope.

So your <cfthread> block won't know what variables like local.pageNo are.

Any error occurring in <cfthread> processing is logged, so you need to look at your logs to see what errors are cropping up.

Adam Cameron
  • 29,677
  • 4
  • 37
  • 78
  • Hey Adam, Perfect answer. I had no idea that errors were being thrown. I just assumed the code I wrote was being bypassed. When I checked the logs it was easy to debug. Cheers – James Privett Jul 18 '14 at 10:43
  • I think this throws everyone the first time. I'm working on some threading stuff this week actually, and it tripped me up just y/day ;-) – Adam Cameron Jul 18 '14 at 11:25
  • My observations cause me to disagree with the first paragraph of this answer. I have used cfthread and when doing so, I was able to access variables in the form and variables scopes from the main thread. – Dan Bracuk Jul 18 '14 at 14:25
  • I don't think Adam was intentionally excluding form and variables scopes; he was merely stating a best practice for making data accessible within the thread. Local scope is not available from within the thread - that was the main point and Adam provided an effective alternative to local scope. Variables scope might also be viable, but the form scope, in this context, would not be a good alternative for this code. – Carl Von Stetten Jul 18 '14 at 15:32