首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用cfthread join获取在cfloop中运行的变量的值

使用cfthread join获取在cfloop中运行的变量的值
EN

Stack Overflow用户
提问于 2010-07-15 00:51:55
回答 3查看 2.3K关注 0票数 5

感谢您的回复!但我还是做不到。我得到的错误是"Element objGet1 is undefined in a Java object of type coldfusion.runtime.VariableScope“。

下面是我的完整代码。我只想转储包含cfhttp信息的每个线程的值。

代码语言:javascript
复制
 http://www.google.com/search?" & "q=Vin+Diesel" & "&num=10" & "&start=") />
代码语言:javascript
复制
<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>

当我在循环中使用after线程连接时。我得到了想要的结果,谢谢!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-07-16 02:38:30

这里发生了两个问题。

正如Zugwalt所指出的,您需要显式地传入您希望在线程范围内引用的变量。他遗漏了CGI变量,这个作用域不存在于你的线程中。因此,我们只传入线程中需要使用的userAgent、strBaseURL和intGet。

第二个问题,一旦加入,你的线程不在变量作用域中,它们在cfthread作用域中,所以我们必须从那里读取它们。

已更正代码:

代码语言:javascript
复制
<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>
票数 6
EN

Stack Overflow用户

发布于 2010-07-15 01:11:33

通常,未限定作用域的变量被放入Variables作用域,因此您可以使用结构括号表示法来引用它们:

代码语言:javascript
复制
Variables['objGet#intGet#']

代码语言:javascript
复制
Variables['objGet'&intGet]

它们基本上都在做同样的事情--只是语法不同而已。

票数 3
EN

Stack Overflow用户

发布于 2010-07-16 02:01:01

在cfthread标记内运行的代码有其自己的作用域。尝试将您希望其访问的变量作为属性进行传递。我喜欢给它起不同的名字,只是为了帮助我保持跟踪。

代码语言:javascript
复制
<!--- 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>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3248513

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档