CFTHREAD执行两次



我有一个调用cfcomponent对象的循环。

    <cfset queue_list = "1,2,3">        
    <cfloop list="#queue_list#" index="z">  
                <cfset args = StructNew()>
                <cfset args.group_num = z>          
<cfset args.client_id = 1>          
                <cfset processdownloads = downloader.ProcessDownload(argumentCollection=args)>
            </cfloop>

该组件具有以下功能:

    <cffunction name="ProcessDownload" access="public" output="false">
        <cfargument name="group_num" type="numeric" required="yes">                     
        <cfargument name="client_id" type="numeric" required="yes">                     
        <cfset variables = arguments>
        <cfthread action="RUN" name="download_#variables.client_id#_#variables.group_num#" priority="high">
                    <cffile action="WRITE" file="#expandpath('downloaddownload_in_process')##variables.group_num#.json" output="#variables.group_num#">           
</cfthread>
</cffunction>

当我运行它时,我收到以下错误:

cfthread标记的属性验证错误。无法创建名为DOWNLOAD_4003_3的线程。线程名称在页面中必须是唯一的
错误发生在第29行。

我不知道为什么,但它好像跑了两次。它是否应该生成一个具有唯一线程名称的新线程,从而避免线程名称冲突?

将group_num作为属性传入,这样您就可以在内部访问它,而不会出现覆盖变量范围的问题。

<cfthread action="RUN" name="download_#arguments.client_id#_#arguments.group_num#" priority="high" group_num="#arguments.group_num#">
    <cffile action="WRITE" file="#expandpath('downloaddownload_in_process')##attributes.group_num#.json" output="#attributes.group_num#">           
</cfthread>

其他人都是对的,问题是你的变量范围。发生的情况是,每个循环都覆盖了变量范围,所以当创建线程时,它会从变量范围中获取线程名称,该名称已经设置为3……所以所有三个线程都可能尝试设置为相同的名称。

你能用参数命名吗?如果不是。。。你可以使用local。并将信息传递到CFThread Creation中。

你在组件内部是正确的,你不能访问参数等,这与组件外部的行为非常不同。

Ben Nadel写了一篇关于这些问题的好文章http://www.bennadel.com/blog/2215-an-experiment-in-passing-variables-into-a-cfthread-tag-by-reference.htm

本照常获胜。

这可能是因为您的CFC代码不是线程安全的。

此:

<cfset variables = arguments>

将函数的参数复制到对象的共享作用域中。如果downloader对象在请求之间共享,那么每个请求都将使用另一个请求的值。

为什么要将参数复制到对象变量范围中?这似乎是一件奇怪的事情。

最新更新