coldfusionrailocfthread

coldfusion (railo) and cfthread not working as I would expect


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?


Solution

  • 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.