使用CFThread加入获取在CFLOOP中运行的变量值
-
15-09-2020 - |
题
谢谢回复!!但我仍然无法做到这一点。我得到的错误是 “Element Objget1在CludFusion”的Java对象中未定义.Runtime.VariaBlescope。“
下面是我的完整代码。我只是想转储包含CFHTTP信息的每个线程的值。
http://www.google.com/search?“&”q= vin +柴油“&”&num= 10“&”&start=“)/>
<cfset intStartTime = GetTickCount() />
<cfloop index="intGet" from="1" to="10" step="1">
<!--- Start a new thread for this CFHttp call. --->
<cfthread action="run" name="objGet#intGet#">
<cfhttp method="GET" url="#strBaseURL##((intGet - 1) * 10)#" useragent="#CGI.http_user_agent#" result="THREAD.Get#intGet#" />
</cfthread>
</cfloop>
<cfloop index="intGet" from="1" to="10" step="1">
<cfthread action="join" name="objGet#intGet#" />
<cfdump var="#Variables['objGet'&intGet]#"><br />
</cfloop>
.
以及在循环内部连接后使用后使用。我得到了预期的结果 谢谢!!
解决方案
这里发生的两个问题。
如zugwalt指出的那样,您需要在主题范围内明确地在您想要引用的变量中传递。他错过了CGI变量,你的线程内不存在该范围。所以我们只能在线程,userAgent,strbaseurl和intget中使用什么。
第二个问题,一旦加入,你的线程没有变量范围,它们处于CFThread范围,所以我们必须从那里读取它们。
更正代码:
<cfloop index="intGet" from="1" to="2" step="1">
<!--- Start a new thread for this CFHttp call. Pass in user Agent, strBaseURL, and intGet --->
<cfthread action="run" name="objGet#intGet#" userAgent="#cgi.http_user_agent#" intGet="#intGet#" strBaseURL="#strBaseURL#">
<!--- Store the http request into the thread scope, so it will be visible after joining--->
<cfhttp method="GET" url="#strBaseURL & ((intGet - 1) * 10)#" userAgent="#userAgent#" result="thread.get#intGet#" />
</cfthread>
</cfloop>
<cfloop index="intGet" from="1" to="2" step="1">
<!--- Join each thread --->
<cfthread action="join" name="objGet#intGet#" />
<!--- Dump each named thread from the cfthread scope --->
<cfdump var="#cfthread['objGet#intGet#']#" />
</cfloop>
. 其他提示
一般来说,未提出的变量放入Variables
范围,因此您可以使用struct括号表示法来引用它们:
Variables['objGet#intGet#']
.
或
Variables['objGet'&intGet]
.
这些都是基本上做同样的事情 - 只是不同的语法。
cfthread标记内的代码有自己的范围。尝试传递您希望它作为属性访问的变量。我喜欢把它命名为不同只是为了帮助我跟踪。
<!--- Start a new thread for this CFHttp call. --->
<cfthread action="run" name="objGet#intGet#" intGetForThread="#intGet#">
<cfhttp method="GET" url="#strBaseURL##((intGetForThread- 1) * 10)#" useragent="#CGI.http_user_agent#" result="THREAD.Get#intGetForThread#" />
</cfthread>
.
不隶属于 StackOverflow