Threaded cfhttp example

216 views
Skip to first unread message

hofar...@houseoffusion.com

unread,
Feb 16, 2015, 7:57:49 AM2/16/15
to ColdFusion Technical Talk

Hello Experts!

I have been trying to create a multi-threaded cfhttp request script, but I have so far failed miserably.

I want to run 10 concurrent threads that call URLS using cfhttp and I want to store the results (basically cfhttp.filecontent) in an array or a similar structure that I can loop once all threads have finished.

Can someone provide me with a working example of this?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Order the Adobe Coldfusion Anthology now!
http://www.amazon.com/Adobe-Coldfusion-Anthology/dp/1430272155/?tag=houseoffusion
Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:360120

hofar...@houseoffusion.com

unread,
Feb 16, 2015, 8:45:48 AM2/16/15
to ColdFusion Technical Talk

This should get you started. The cfthread join waits the specific timeout
for the threads in the name list to finish. If not all threads are
complete, things continue on, so you may want to perform checks against the
threads returned in cfthread to ensure the values exists, etc.

<cfthread action="run" aUrl="http://www.google.com" name="t1">
<cfhttp method="get" url="#aUrl#" timeout='10' />
<cfset thread.rtn = cfhttp.fileContent.length()>
</cfthread>

<cfthread action="run" aUrl="http://www.google.com" name="t2">
<cfhttp method="get" url="#aUrl#" timeout='10' />
<cfset thread.rtn = cfhttp.fileContent.length()>
</cfthread>

<cfthread action='join' name='t1,t2' timeout='5000'></cfthread>

<cfdump var="#cfthread#">

Byron



On Mon, Feb 16, 2015 at 7:57 AM, Michael Christensen <m...@travelmarket.com>
wrote:
Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:360121

hofar...@houseoffusion.com

unread,
Feb 20, 2015, 5:59:34 PM2/20/15
to ColdFusion Technical Talk

Here try this. I snipped it out of something I use to build static pages
with CFHTTP. It builds thousands of them based on query output. I wanted
it to run multiple threads at once but not so many it fried CF. So the
code keeps track of how many threads are running and limits their number to
a value you specify. variables.threadcount sets the number of threads to
allow to run concurrently. For your routine you could have the urls you
want to canvas in a db, query them as seen below and then reference the
query output's current row in the loop via the loopCounter variable as
shown.

<cfscript>
variables.threadArray=arrayNew(1);
variables.threadCount=3;
</cfscript>
<cfquery
name="getData"
datasource="#server.DSN#"
username="#server.userName#"
password="#server.password#">
SELECT
fileName.primaryKey

FROM
fileName
WHERE
0=0
ORDER BY
fileName.primaryKey ASC
</cfquery>
<cfset variables.loopCounter=0>
<cfloop
condition="variables.loopCounter LT getData.recordCount">
<!---
count the threads that are currently live
--->
<cfset variables.threadsLive=arrayLen(variables.threadArray)>
<!---
Do we have an available thread?
--->
<cfif variables.threadsLive lt variables.threadCount>
<!---
A thread is available. Increment the loopCounter and give it a name
--->
<cfset variables.loopCounter=variables.loopCounter+1>
<cfset variables.thisThreadID=createUUID()>
<cfset
temp=arrayAppend(variables.threadArray,variables.thisThreadID)>
<!---
create the thread whose name we specified and have reserved
--->
<cfthread
name="#variables.thisThreadID#"
action="run">
<!---
CF Code to be run inside the thread goes here.
This next cfset is just a dummy
--->
<cfset variables.foo=getdata.ID[variables.loopCounter]>
<!---
remove the now-completed thread from the "live" list
--->
<cfset
temp=arrayDeleteAt(variables.threadArray,arrayFindNoCase(variables.threadArray,variables.thisThreadID))>
</cfthread>
</cfif>
</cfloop>


--
--m@Robertson--
Janitor, The Robertson Team
mysecretbase.com


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Order the Adobe Coldfusion Anthology now!
http://www.amazon.com/Adobe-Coldfusion-Anthology/dp/1430272155/?tag=houseoffusion
Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:360142
Reply all
Reply to author
Forward
0 new messages