背景:
这是从外部站点检索数据(外部站点提供用于通过 Web 服务检索数据的 API)并使用新信息更新数据库的计划作业的一部分。它正在检索大约 3,500 个数据项。我当前的计划作业创建CFThread
了一次运行 10 个线程的任务块,并在开始下一个 10 个线程块之前加入它们。
代码:
<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 会将额外的线程排队并在有时间时运行它们吗?