如果下載看起來沒有響應,您可以創建另一個線程來觀察下載進度並使應用程序崩潰。由於網:: SFTP,您可以在自定義處理程序傳遞給download!
方法,你可以設置觀察者線程這樣的:
class CustomHandler
def extend_time
@crash_time = Time.now + 30
end
# called when downloading has started
def on_open(downloader, file)
extend_time
downloader_thread = Thread.current
@watcher_thread = Thread.new{
while true do
if Time.now > @crash_time
downloader_thread.raise "Downloading appears unresponsive. Network disconnected?"
end
sleep 5
end
}
end
# called when new bytes are downloaded
def on_get(downloader, file, offset, data)
extend_time
end
# called when downloading is completed
def on_close(downloader, file)
@watcher_thread.exit
end
end
而且不要忘記在這樣的定製處理器來傳遞:
sftp.download!(remote_path, local_path, :progress => CustomHandler.new)
在你的程序被調用爲腳本的情況下,這很少是一個問題,net :: sftp有默認的超時時間,但它是用於連接的初始階段,我想。因此,如果建立的連接掛起,則需要從父線程或另一個進程(保姆)中斷它。可能是解決這個問題最簡單的方法。如果您的應用程序運行時間長於線程,則是解決此問題的最佳方法。 – Istvan 2012-11-22 07:09:13