Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8923053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:00:36+00:00 2026-06-15T07:00:36+00:00

When downloading a large file with python, I want to put a time limit

  • 0

When downloading a large file with python, I want to put a time limit not only for the connection process, but also for the download.

I am trying with the following python code:

import requests

r = requests.get('http://ipv4.download.thinkbroadband.com/1GB.zip', timeout = 0.5, prefetch = False)

print r.headers['content-length']

print len(r.raw.read())

This does not work (the download is not time limited), as correctly noted in the docs: https://requests.readthedocs.org/en/latest/user/quickstart/#timeouts

This would be great if it was possible:

r.raw.read(timeout = 10)

The question is, how to put a time limit to the download?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T07:00:39+00:00Added an answer on June 15, 2026 at 7:00 am

    And the answer is: do not use requests, as it is blocking. Use non-blocking network I/O, for example eventlet:

    import eventlet
    from eventlet.green import urllib2
    from eventlet.timeout import Timeout
    
    url5 = 'http://ipv4.download.thinkbroadband.com/5MB.zip'
    url10 = 'http://ipv4.download.thinkbroadband.com/10MB.zip'
    
    urls = [url5, url5, url10, url10, url10, url5, url5]
    
    def fetch(url):
        response = bytearray()
        with Timeout(60, False):
            response = urllib2.urlopen(url).read()
        return url, len(response)
    
    pool = eventlet.GreenPool()
    for url, length in pool.imap(fetch, urls):
        if (not length):
            print "%s: timeout!" % (url)
        else:
            print "%s: %s" % (url, length)
    

    Produces expected results:

    http://ipv4.download.thinkbroadband.com/5MB.zip: 5242880
    http://ipv4.download.thinkbroadband.com/5MB.zip: 5242880
    http://ipv4.download.thinkbroadband.com/10MB.zip: timeout!
    http://ipv4.download.thinkbroadband.com/10MB.zip: timeout!
    http://ipv4.download.thinkbroadband.com/10MB.zip: timeout!
    http://ipv4.download.thinkbroadband.com/5MB.zip: 5242880
    http://ipv4.download.thinkbroadband.com/5MB.zip: 5242880
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am needing to download a very large file via PHP, the last time
I want to download a large file and concurrently handle other things. However, luasocket.http
I have a problem when downloading a large file (around 400mb) with a web
I need to write a WCF method for downloading large file from server like
I am creating an application where i am downloading a large file from dropbox
I've found a sample to download a large data file at the following link,
For a NLP project of mine, I want to download a large number of
I'm downloading a rather large file on the SD card of an Android device
I have to write a php script to download potentially large files. The file
I am downloading a large file from the internet within my code and would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.