質問

ftp経由でファイルのリストをアップロードするrakeタスクがあります。スレッド化せずにコピーすることは正常に機能しますが、複数の同時アップロードを実行できれば高速になります。 (私はルビーとマルチスレッドを初めて使用するので、すぐに動作しなかったのも驚くことではありません。)

私は持っています:

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