cfhopp中的cfhttp?使用cfthread [英] cfhttp in cfloop limit? use cfthread
问题描述
我目前正在尝试开发一个API,而我刚才所处的阶段是使用完整的数据集(ID,名字,姓氏,dob等)来填充表格。
I'm currently trying to develop an API and the stage where I'm at just now is to populate a table with a full data set (ID, first name, last name, dob etc).
我写的方法是使用一个从1到500,000的cfloop(因为我不知道ID范围从和到)和在每个cfloop内我调用一个函数
The way I've written this is to use a cfloop from 1 to 500,000 (as I don't know what range the IDs range from and to) and within each cfloop I call a function that makes a cfhttp request to the server and retrieve the content.
然后我反序列化返回的JSON,调用一个函数来查询我的表,查看当前项目ID是否已经存在。存在,如果不是,调用一个函数插入记录。
I then deserialize the returned JSON, call a function to query my table to see if the current item ID already exists and, if not, call a function to insert the record.
然而,cfloop似乎停止了300个请求标记,所以我想知道是否有更好的方法做我在做什么?也许通过使用CFTHREAD标签,我从来没有使用过任何经验。
However the cfloop seems to stop around the 300 request mark so I was wondering if there was a better way to do what I'm doing? Perhaps by using the CFTHREAD tag which I've never had any experience of using.
这段代码如下:
<cfset Variables.url = "someurl.html" />
<cfloop from=100000 to=500000 index="itemNo">
<cfset Variables.itemID = itemNo />
<cfset getItemData = Application.cfcs.Person.getPersonData(Variables.url,Variables.itemID) />
<cfif StructKeyExists(Variables,"getPersonData.FileContent")>
<cfset Variables.getPersonData = DeserializeJSON(getPersonData.FileContent)>
<cfscript>
// CHECK IF PERSON ALREADY IN DATABASE
Variables.getPerson = Application.cfcs.Person.getPersonRecord(Variables.itemID);
// INSERT ITEM IN TO TABLE
Variables.DOB = CreateDate(Year(Variables.getPersonData.Item.DateOfBirth.Year),Month(Variables.getPersonData.Item.DateOfBirth.Month),Day(Variables.getPersonData.Item.DateOfBirth.Day));
Variables.insPerson = Application.cfcs.Person.insPerson(Variables.getPersonData.personID,Variables.getPersonData.Item.FirstName,Variables.getPersonData.Item.LastName,Variables.getPersonData.Item.CommonName,Variables.DOB);
</cfscript>
</cfif>
</cfloop>
推荐答案
您需要拆分呼叫。创建一个简单的htlm页面,使javascript在xmlhttprequest。我没有测试下面的例子,但它应该工作。
Yes it is possible. You need to split up the call. Create a simple htlm page which makes a xmlhttprequest in javascript. I haven't tested the example below but it should work.
<script>
var itemNo= 1;
function download()
{
var xhr = new XMLHttpRequest();
xhr.open("GET", "getdata.cfm?itemNo="+itemNo, true);
xhr.onload = function (e) {
if (xhr.readyState === 4) {
if (xhr.status === 200)
{
itemNo++;
if(itemNo<=500000) download();
}
else
{
itemNo++;
// Error handling
}
}
};
xhr.onerror = function (e) {
itemNo++;
// Error handling
};
xhr.send(null);
}
</script>
在请求的页面上调用cfhttp请求的对象。
On the requested page make the the call to the object that makes the cfhttp request.
<!--- getdata.cfm --->
<cfset Variables.url = "someurl.html" />
<cfset Variables.itemID = itemNo />
<cfset getItemData = Application.cfcs.Person.getPersonData(Variables.url,Variables.itemID) />
<cfif StructKeyExists(Variables,"getPersonData.FileContent")>
<cfset Variables.getPersonData = DeserializeJSON(getPersonData.FileContent)>
<cfscript>
// CHECK IF PERSON ALREADY IN DATABASE
Variables.getPerson = Application.cfcs.Person.getPersonRecord(Variables.itemID);
// INSERT ITEM IN TO TABLE
Variables.DOB = CreateDate(Year(Variables.getPersonData.Item.DateOfBirth.Year),Month(Variables.getPersonData.Item.DateOfBirth.Month),Day(Variables.getPersonData.Item.DateOfBirth.Day));
Variables.insPerson = Application.cfcs.Person.insPerson(Variables.getPersonData.personID,Variables.getPersonData.Item.FirstName,Variables.getPersonData.Item.LastName,Variables.getPersonData.Item.CommonName,Variables.DOB);
</cfscript>
</cfif>
在请求的页面上,您可以使用cfhtread同时进行多个http请求。您可以在这里查看有关将cfthread与cfhttp一起使用的更多信息 http://www.bennadel.com/blog/749-Learning-ColdFusion-8-CFThread-Part-II-Parallel-Threads.htm
On the requested page you could use cfhtread to make multiple http request simultaneously. You can look here for more information about using cfthread together with cfhttp http://www.bennadel.com/blog/749-Learning-ColdFusion-8-CFThread-Part-II-Parallel-Threads.htm
这篇关于cfhopp中的cfhttp?使用cfthread的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!