2016-06-17 305 views
0


首先,我是Ruby的初學者,對Ruby管理代碼的方式不熟悉,所以我確實希望問題是我不知道如何使用紅寶石正確。
我遇到的問題是死鎖,但我沒有在我的程序中使用任何線程。另外,錯誤只發生在每1000到1500次函數調用中,這使得查明和糾正非常困難。
下面是完整的錯誤消息時出現該問題:
Ruby:open會導致死鎖

/usr/lib/ruby/2.3.0/timeout.rb:95:in `join': No live threads left. Deadlock? (fatal) 
    from /usr/lib/ruby/2.3.0/timeout.rb:95:in `ensure in block in timeout' 
    from /usr/lib/ruby/2.3.0/timeout.rb:95:in `block in timeout' 
    from /usr/lib/ruby/2.3.0/timeout.rb:101:in `timeout' 
    from /usr/lib/ruby/2.3.0/net/http.rb:878:in `connect' 
    from /usr/lib/ruby/2.3.0/net/http.rb:863:in `do_start' 
    from /usr/lib/ruby/2.3.0/net/http.rb:852:in `start' 
    from /usr/lib/ruby/2.3.0/open-uri.rb:319:in `open_http' 
    from /usr/lib/ruby/2.3.0/open-uri.rb:737:in `buffer_open' 
    from /usr/lib/ruby/2.3.0/open-uri.rb:212:in `block in open_loop' 
    from /usr/lib/ruby/2.3.0/open-uri.rb:210:in `catch' 
    from /usr/lib/ruby/2.3.0/open-uri.rb:210:in `open_loop' 
    from /usr/lib/ruby/2.3.0/open-uri.rb:151:in `open_uri' 
    from /usr/lib/ruby/2.3.0/open-uri.rb:717:in `open' 
    from /usr/lib/ruby/2.3.0/open-uri.rb:35:in `open' 
    from /home/mat/travail_perso/RUBY/MangaScrapp_github/sources/utils.rb:85:in `get_pic' 
    from /home/mat/travail_perso/RUBY/MangaScrapp_github/mangafox/MF_download.rb:87:in `page_link' 
    from /home/mat/travail_perso/RUBY/MangaScrapp_github/mangafox/MF_download.rb:116:in `chapter_link' 
    from /home/mat/travail_perso/RUBY/MangaScrapp_github/mangafox/MF_download.rb:142:in `chapter' 
    from /home/mat/travail_perso/RUBY/MangaScrapp_github/mangafox/MF_update.rb:57:in `block in MF_manga_missing_chapters' 
    from /home/mat/travail_perso/RUBY/MangaScrapp_github/mangafox/MF_update.rb:45:in `reverse_each' 
    from /home/mat/travail_perso/RUBY/MangaScrapp_github/mangafox/MF_update.rb:45:in `MF_manga_missing_chapters' 
    from /home/mat/travail_perso/RUBY/MangaScrapp_github/mangafox/MF_update.rb:80:in `MF_update' 
    from /home/mat/travail_perso/RUBY/MangaScrapp_github/sources/update.rb:5:in `update_manga' 
    from /home/mat/travail_perso/RUBY/MangaScrapp_github/sources/update.rb:15:in `block in update_all' 
    from /home/mat/travail_perso/RUBY/MangaScrapp_github/sources/update.rb:14:in `each' 
    from /home/mat/travail_perso/RUBY/MangaScrapp_github/sources/update.rb:14:in `update_all' 
    from /home/mat/travail_perso/RUBY/MangaScrapp_github/sources/update.rb:22:in `update' 
    from ./MangaScrap.rb:28:in `<main>' 

鏈接到完整的程序是在這裏:https://github.com/Hellfire01/MangaScrap
問題發生在使用開放的3種不同的方法,這裏是一個墜毀這段時間:

# conect to link and download picture 
def get_pic(link) 
    safe_link = link.gsub(/[\[\]]/) { '%%%s' % $&.ord.to_s(16) } 
    tries ||= 20 
    begin 
    page = open(safe_link, "User-Agent" => "Ruby/#{RUBY_VERSION}") 
    rescue URI::InvalidURIError => error 
    puts "Warning : bad url" 
    puts link 
    puts "message is : " + error.message 
    return nil 
    rescue => error 
    if tries > 0 
    tries -= 1 
    sleep(0.2) 
    retry 
    else 
     puts 'could not get picture ' + safe_link + ' after ' + $nb_tries.to_s + ' tries' 
     puts "message is : " + error.message 
     return nil 
    end 
    end 
    sleep(0.2) 
    return page 
end 

下面是該文件的鏈接:https://github.com/Hellfire01/MangaScrap/blob/master/sources/utils.rb

我想知道的是:
- 我該如何解決這個錯誤?
- 如果我無法修復這個錯誤,是否可以使用open-uri的替代方法?

任何幫助是非常值得歡迎

+1

僅供參考,爲原因一個線程錯誤,儘管沒有使用線程是ruby超時庫正在被net/http使用(反過來由open-uri使用),並且超時使用線程 –

+1

是的,我把這麼多,但我仍然不知道如何修復問題:/ –

+1

[curb](https://github.com/taf2/curb)比原始的Net :: HTTP更令人愉快。 – tadman

回答

2

你不在這裏捕獲所有異常。當在rescue之後沒有指定任何內容時,這意味着您正在捕獲不在異常的層次結構根目錄中的StandardError

如果你想確保你趕上所有異常並嘗試重新打開一個URL(或您想任何行爲),你想要做的是: rescue Exception => error