這是從外部網站(外部網站提供檢索通過Web服務數據的API)檢索數據並更新新的信息數據庫的計劃作業的一部分。它正在檢索大約3,500個數據項。我目前的計劃作業產生的,在一次運行10個線程和啓動的10
下一個代碼塊之前加入他們CFThread
任務塊:
<cfset local.nMaxThreadCount = 10>
<!---retrieve a query that contains the items that need to be updated, approximately 3,500 items--->
<cfset local.qryItemsNeedingUpdate = getItemsNeedingUpdate(dtMostRecentItemPriceDate = local.qryMostRecentItemPriceDate.dtMostRecentItemPrice[1])>
<cfset local.nThreadBlocks = Ceiling(local.qryItemsNeedingUpdate.RecordCount/local.nMaxThreadCount)>
<cftry>
<cfloop index="local.nThreadBlock" from="1" to="#local.nThreadBlocks#">
<cfif local.nThreadBlock EQ local.nThreadBlocks>
<cfset local.nThreadCount = local.qryItemsNeedingUpdate.RecordCount MOD local.nMaxThreadCount>
<cfelse>
<cfset local.nThreadCount = local.nMaxThreadCount>
</cfif>
<cfset local.lstThreads = "">
<cfloop index="local.nThread" from="1" to="#local.nThreadCount#">
<cfset local.nQryIdx = ((local.nThreadBlock - 1) * local.nMaxThreadCount) + local.nThread>
<cfset local.vcThreadName = "updateThread#local.qryItemsNeedingUpdate.nItemID[local.nQryIdx]#">
<cfset local.lstThreads = ListAppend(local.lstThreads, local.vcThreadName)>
<!---create the attributes struct to pass to a thread--->
<cfset local.stThread = StructNew()>
<cfset local.stThread.action = "run">
<cfset local.stThread.name = local.vcThreadName>
<cfset local.stThread.nItemID = local.qryItemsNeedingUpdate.nItemID[local.nQryIdx]>
<!---spawn thread--->
<cfthread attributecollection="#local.stThread#">
<cfset updateItemPrices(nItemID = attributes.nItemID)>
</cfthread>
</cfloop>
<!---join threads--->
<cfthread action="join" name="#local.lstThreads#" />
</cfloop>
<cfcatch type="any">
<cflog text="detailed error message logged here..." type="Error" file="myDailyJob" application="yes">
</cfcatch>
</cftry>
問題:
後臺進程需要這種邏輯嗎?那就是需要CFThread action="join"
?線程中不顯示任何內容,並且線程是獨立的(不要依賴其他線程或派生它們的進程)。線程更新數據庫中的價格並死亡。是否需要限制線程,即一次運行10個線程並加入?該進程是否可以一次循環並創建所有3,500個線程? ColdFusion會將多餘的線程排隊並按時運行嗎?