2009-09-11 18 views
1

我有一個rake任務,通過ftp上傳文件列表。無需線程複製工作正常,但如果我可以進行多個併發上傳,速度會更快。 (我是新來的Ruby和多線程,所以沒有驚喜也沒有了蝙蝠的工作權利。)FTP的Ruby線程(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 

所以它看起來像每個線程開始上傳文件,然後死亡沒有錯誤。 我在做什麼錯?

回答

0

的問題的根源是固定的加入:在file_slice循環結束

threads.each { |t| t.join } 

之後。

感謝JRL幫助我找到例外!

2

如果abort_on_exception爲false並且調試標誌未啓用(默認),則未處理的異常會終止當前線程。直到您在引發它的線程上發出連接之前,您甚至不知道它。因此,您可以執行連接或更改調試標誌,並且如果確實引發了異常,您應該得到異常。

+0

太棒了!我甚至不知道調試標誌。 – willoller 2009-09-13 04:41:52

+0

@JRL:增加了$ DEBUG = true和abort_on_exception。我遇到了錯誤,即「IOError流關閉」。我認爲這是一個鎖定問題,但即使當我只有一個線程 - 其他任何建議? 謝謝! – willoller 2009-09-14 18:41:32

+1

@willoller:你是否在調用File.open的關聯塊?如果是這樣,當該塊終止時,該文件將自動關閉。如果你不想要這個,不要將一個塊與File.open方法調用關聯起來,或者使用File.new。 – JRL 2009-09-14 18:54:14