Domanda

Ho un'attività di rake che carica un elenco di file tramite ftp. La copia senza threading funziona correttamente, ma sarebbe più veloce se potessi eseguire più caricamenti simultanei. (Sono nuovo di rubino e multithreading, quindi nessuna sorpresa non ha funzionato subito.)

Ho:

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

Il mio output è:

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

Quindi sembra che ogni thread inizi a caricare un file e poi muoia senza errori. Cosa sto sbagliando?

È stato utile?

Soluzione 2

La radice del problema è stata risolta aggiungendo:

threads.each { |t| t.join }

al termine del ciclo file_slice .

Grazie a JRL per avermi aiutato a trovare l'eccezione!

Altri suggerimenti

Se abort_on_exception è falso e il flag di debug non è abilitato (impostazione predefinita) un'eccezione non gestita uccide il thread corrente. Non lo sai nemmeno fino a quando non emetti un join sul thread che lo ha generato. Quindi puoi fare un join o cambiare il flag di debug e dovresti ottenere l'eccezione se ne viene effettivamente lanciato uno.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top