Ruby FTP extremely slow under Windows XP

2019-04-12 15:56发布

问题:

A couple of weeks ago, I wrote a simple Ruby script to test a couple of FTP commands in a Windows XP environment. Everything worked as expected, and I wasn't even aware of the time taken for the code to run (I'd guess 3-4 seconds at the very most.)

A few days ago, a much more involved Ruby application I'm developing started running very slowly. As I investigated the issue, I isolated the problem to the FTP commands. I've now rerun the original test script, and it takes over two minutes to run. Command-line FTP is essentially instantaneous.

No files in the ruby directory structure have been changed. I do not believe any new applications have been installed - certainly no other applications appear to be running.

Can anyone suggest why the following code should run so slowly? Manually timing the intervals between print statements suggest the nlst and ls take about 65 seconds each! The profiler gives a much more plausible total ms/call of 16 for nlst and 31 for ls.

require 'net/ftp'

Net::FTP.open("ip_redacted", "user_redacted", "password_redacted") do |ftp|
    ftp.chdir("dir_redacted")

    files = ftp.nlst
    print "files = #{files.sort!}\n"
    list = ftp.ls
    print "list = #{list}\n"

    file = "filename_redacted"

    size = ftp.size(file)
    print "size = #{size}\n"

end

回答1:

From a google search:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/112910

Hope this helps.



回答2:

I had a similar issue and found Passive mode increased the speed, eg. ftp.passive=true.



回答3:

Try removing the #sort! (just puts "files = #{files}") since that can be fairly expensive if there's a bunch of files in the directory. This would account for the large lag around the #nlst and the #ls



标签: windows ruby ftp