PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Efficient file downloading
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Efficient file downloading

Réponse
 
LinkBack Outils de la discussion
Vieux 22/02/2008, 01h54   #1
Kyle Hunter
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Efficient file downloading

Hello,

I'm using open-uri to download files using a buffer. It seems very
inefficient in terms of resource usage (CPU is ~10-20% in usage).

If possible, I'd like some suggestions for downloading a file which
names the outputted file the same as the URL, and does not actually
write if the file comes out to a 404 (or some other exception hits).

Current code:
BUFFER_SIZE=4096
def download(url)
from = open(url)
if (buffer = from.read(BUFFER_SIZE))
puts "Downloading #{url}"
File.open(url.split('/').last, 'wb') do |file|
begin
file.write(buffer)
end while (buffer = from.read(BUFFER_SIZE))
end
end
end
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 22/02/2008, 01h55   #2
Kyle Hunter
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Efficient file downloading

To clarify, I mean the file-name should be the same as it is on the web,
not the same as the URL.
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 22/02/2008, 02h24   #3
James Tucker
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Efficient file downloading


On 22 Feb 2008, at 01:54, Kyle Hunter wrote:

> Hello,
>
> I'm using open-uri to download files using a buffer. It seems very
> inefficient in terms of resource usage (CPU is ~10-20% in usage).
>
> If possible, I'd like some suggestions for downloading a file which
> names the outputted file the same as the URL, and does not actually
> write if the file comes out to a 404 (or some other exception hits).
>
> Current code:
> BUFFER_SIZE=4096


Try making that a lot lot bigger.

>
> def download(url)
> from = open(url)
> if (buffer = from.read(BUFFER_SIZE))
> puts "Downloading #{url}"
> File.open(url.split('/').last, 'wb') do |file|
> begin
> file.write(buffer)
> end while (buffer = from.read(BUFFER_SIZE))
> end
> end
> end
> --
> Posted via http://www.ruby-forum.com/.
>



  Réponse avec citation
Vieux 22/02/2008, 02h34   #4
Kyle Hunter
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Efficient file downloading

James Tucker wrote:
> On 22 Feb 2008, at 01:54, Kyle Hunter wrote:
>
>> BUFFER_SIZE=4096

> Try making that a lot lot bigger.


Doh! Thanks James. Brings it down to much more reasonable usage. I
totally overlooked that very small buffer size that was set - thanks.
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 22/02/2008, 22h06   #5
fedzor
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Efficient file downloading


On Feb 21, 2008, at 8:54 PM, Kyle Hunter wrote:

> Hello,
>
> I'm using open-uri to download files using a buffer. It seems very
> inefficient in terms of resource usage (CPU is ~10-20% in usage).
>
> If possible, I'd like some suggestions for downloading a file which
> names the outputted file the same as the URL, and does not actually
> write if the file comes out to a 404 (or some other exception hits).
>
> Current code:
> BUFFER_SIZE=4096
> def download(url)
> from = open(url)
> if (buffer = from.read(BUFFER_SIZE))
> puts "Downloading #{url}"
> File.open(url.split('/').last, 'wb') do |file|
> begin
> file.write(buffer)
> end while (buffer = from.read(BUFFER_SIZE))
> end
> end
> end


$ sudo gem install snoopy
$ snoopy http://en.wikipedia.org/wiki/Main_Page
=> file Main_Page

Ta dah! there's a lot of magic behind it right now, and torrentz
don't work (fixed on my machine, need to release it). It does
segmented downloading, ideal for large files. For smaller ones, it
still works fine.

The problem with open-uri is this: it downloads the whole thing to
your tmp directory first, so using the BUFFER_SIZE thing won't
actually .

snoopy won't not write the file if there's an error.

-------------------------------------------------------|
~ Ari
Some people want love
Others want money
Me... Well...
I just want this code to compile


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 06h12.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11916 seconds with 13 queries