Ruby Net::FTP Timeout Threads

后端 未结 1 1677
逝去的感伤
逝去的感伤 2020-12-31 17:17

I was trying to speed up multiple FTP downloads by using threaded FTP connections. My problem is that I always have threads hang. I am looking for a clean way of either te

1条回答
  •  执念已碎
    2020-12-31 18:16

    The trick for me that worked was to use ruby's Timeout.timeout to ensure the FTP connection was not hanging.

    begin
        Timeout.timeout(10) do
            ftp.getbinaryfile(rmls_path, local_path)
        end
        # ...
    rescue Timeout::Error
        errors << "#{thread_num}> File download timed out for: #{rmls_path}"
        puts errors.last
    rescue
        errors << "unable to get file > ftp reponse: #{ftp.last_response}"
        # ...
    end
    

    Hanging FTP downloads were causing my threads to appear to hang. Now that the threads are no longer hanging, I can use the more proper way of dealing with threads:

    threads.each { |t| t.join }
    

    rather than the ugly:

    # If @last_updated has not been updated on the server in over 20 seconds, wait 3 seconds and check again
    while Time.now < @last_updated + 20 do
        sleep 3
    end
    # threads are hanging so joining the threads does not work.
    threads.each { |t| t.kill }
    

    0 讨论(0)
提交回复
热议问题