我有一个rake任务,通过ftp上传文件列表。没有线程的复制工作正常,但如果我可以进行多个并发上传,它会更快。 (我是红宝石和多线程的新手,所以毫不奇怪它不能立即起作用。)

我有:

files.each_slice(files.length / max_threads) do |file_set|
    threads << Thread.new(file_set) do |file_slice|
        running_threads += 1
        thread_num = running_threads
        thread_num.freeze
        puts "making thread # #{thread_num}"

        file_slice.each do |file|
            file.freeze
            if File.directory?(file) 
            else
                puts file.pathmap("#{$ftpDestination}%p")
                ftp.putbinaryfile(file, file.pathmap("#{$ftpDestination}%p")) 
            end
        end
    end    
end

我的输出是:

making thread # 1
/test/./1column-ff-template.aspx
making thread # 2
making thread # 3
/test/./admin/footerContent.aspx
/test/./admin/contentList.aspx
making thread # 4
/test/./3columnTemplate.ascx
making thread # 5
/test/./ascx/dashboard/dash.ascx
making thread # 6
/test/./ascx/Links.ascx
making thread # 7
/test/./bin/App_GlobalResources.dll
making thread # 8
/test/./bin/App_Web__foxtqrr.dll
making thread # 9
/test/./GetPageLink.ascx

所以看起来每个线程都开始上传文件,然后在没有错误的情况下死掉。 我做错了什么?

有帮助吗?

解决方案 2

问题的根源是通过添加:

来解决的
threads.each { |t| t.join }
file_slice 循环结束后

感谢JRL帮助我找到了例外!

其他提示

如果 abort_on_exception 为false且未启用调试标志(默认),则未处理的异常会终止当前线程。在你提出它的线程上发出连接之前,你甚至都不知道它。因此,您可以进行连接或更改调试标志,如果确实抛出了异常,则应该获得异常。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top