[Interest] Throttle downloads

Bob Hood bhood2 at comcast.net
Tue Dec 29 18:11:47 CET 2015


On 12/29/2015 9:32 AM, Jason Kretzer wrote:
>
> Hello all,
>
> I have an application that must download files in the background.  I 
> separate this process off into a separate thread so that it does not 
> interfere with the UI.  Unfortunately, when it starts downloading, it uses 
> the full pipe that it has access to and adversely affects the rest of the 
> network – causing other internet connections to slow to a crawl.  The 
> application is installed on networks which I do not control and cannot 
> throttle it that way.  What I would like to do is have the application not 
> eat so much bandwidth during the download.
>
> The code I am using to download is pretty run of the mill (error checks 
> removed for brevity):
>
> void ContentDownloader::downloadFile(QUrl url, QString filename)
>
> {
>
>     QNetworkRequest request(url);
>
>     QNetworkAccessManager* _manager = new QNetworkAccessManager(this);
>
>     QNetworkReply* reply = _manager->get(request);
>
>     QEventLoop loop;
>
>     connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
>
>     loop.exec();
>
>     QFile file(filename);
>
>     file./open/(QIODevice::/WriteOnly/);
>
>     file.write(reply->readAll());
>
>     file./close/();
>
> }
>
> Thoughts on how to make it “slow down”?  I have been toying with the 
> setReadBufferSize in the reply but does not really seem to change anything.
>
> Thanks!
>
>

Set a timer to read chunks of the file each time it expires, and send that 
chunk down the pipe with your call to file.write().  By adjusting the timer's 
timeout value and the amount of data you read/send, you can effectively 
throttle it yourself to a comfortable bytes-per-second setting.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20151229/907647e1/attachment.html>


More information about the Interest mailing list