代码之家  ›  专栏  ›  技术社区  ›  Deepak Yadav

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

  •  5
  • Deepak Yadav  · 技术社区  · 15 年前

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

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

    http://www.google.com/search?“&”q=Vin+柴油机“&”&数值=10“&”&开始=“)/>

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

    谢谢!!

    3 回复  |  直到 11 年前
        1
  •  6
  •   Anthony    15 年前

    这里发生了两个问题。

    正如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
  •  3
  •   Peter Boughton    15 年前

    一般来说,非范围变量被放入 Variables 范围,因此可以使用结构括号表示法来引用它们:

    Variables['objGet#intGet#']
    

    Variables['objGet'&intGet]
    

    它们基本上是做相同的事情-只是不同的语法。

        3
  •  0
  •   Aaron Silverman    15 年前

    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>
    

    推荐文章