【问题标题】:Get value of variables running in cfloop using cfthread join使用 cfthread join 获取在 cfloop 中运行的变量的值
【发布时间】:2023-03-24 08:40:02
【问题描述】:

感谢回复!!但我仍然无法做到。我得到的错误是 “元素 objGet1 在类型为coldfusion.runtime.VariableScope 的Java 对象中未定义。”

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

http://www.google.com/search?" & "q=Vin+Diesel" & "&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>

当我在循环内使用线程加入后。我得到了想要的结果 谢谢!!

【问题讨论】:

    标签: coldfusion cfloop cfthread


    【解决方案1】:

    这里发生了两个问题。

    正如 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>
    

    【讨论】:

    • 很好地抓住了安东尼!我没有仔细研究整个问题,但幸运的是你真的解决了!
    【解决方案2】:

    一般情况下,无作用域的变量被放入Variables 作用域,因此您可以使用结构括号表示法来引用它们:

    Variables['objGet#intGet#']
    

    Variables['objGet'&intGet]
    

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

    【讨论】:

    • 嗯,你能确认如果你放 它确实会转储第一个吗?另外,尝试将cfthread 中的name 设置为"variables.objGet#intGet#" - 不是必须的,但我还不需要使用cfthread,所以不完全确定它的行为方式。
    【解决方案3】:

    在 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-31
      相关资源
      最近更新 更多