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

  • Home
  • SEARCH
  • 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 7732387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:43:05+00:00 2026-06-01T06:43:05+00:00

This is a two part question. The first is how do you properly close

  • 0

This is a two part question. The first is how do you properly close a connection with urllib2? I have seen a number of examples and I have adopted the best solution I could find. However, there appears to be a problem closing the files.

Currently I use contextlib’s closing() as follows:

    try:
        with closing(self.opener.open(self.address, 
                                      None, 
                                      self.timeout)) as page:
            self.data = page.read()
    except:
        # bail out..

However, I still get “too many open files” error after a long time on OSX. I used ulimit to increase files to 2000 and above. I also set the kernel’s max files up to >40,000. I should note, the object this method is located is not disposed of and it remains around for the life of the program. However, I only keep the “data” stored in the object along with the address and timeout. I don’t keep the file-like object stored. I thought the problem may be references but I don’t believe so because I never store a reference to the file-like object directly, only the data from read(). These objects are reused and reloaded with new data each time a thread pulls a url out of the Queue.

I only open roughly 50 connections at a time. I don’t quite understand how I could run out of files. Also, when I run out of files netstat begins to crap out with malloc errors:

  netstat(439) malloc: *** mmap(size=18446744073708605440) failed (error code=12)
  *** error: can't allocate region
  *** set a breakpoint in malloc_error_break to debug
  netstat: malloc 18446744073708605396 bytes: Cannot allocate memory

I also can’t find a way to reset the connections and get netstat back to normal without shutting down.

netstat -m

$ netstat -m
475/3803 mbufs in use:
    475 mbufs allocated to data
    3328 mbufs allocated to caches
407/3814 mbuf 2KB clusters in use
0/577 mbuf 4KB clusters in use
0/12 mbuf 16KB clusters in use
11242 KB allocated to network (8.3% in use)
0 requests for memory denied
0 requests for memory delayed
0 calls to drain routines

I am having trouble locating the error but I believe the connections are not being closed in a timely manner and I am well aware the connections are not being re-used even when connecting to a single domain (I would like that). That is the second part of the question. How can someone reuse a connection with urllib2?

I have multiple threads getting urls from a Queue and each retrieve the data via this kind of routine. If possible, I would like to reuse the connection if it has been opened by another thread. The only data shared between threads is the URL queue. I have looked at other modules but they appear to need more sharing of data than just a url.

  • 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-01T06:43:06+00:00Added an answer on June 1, 2026 at 6:43 am

    I would recommend dropping urllib2, and trying out the fantastic Requests library.

    It automatically takes care of reusing and closing connections. (docs)

    You might also be interested in its support for making asynchronous requests. (docs)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a two-part question: First, I am interested to know what the best
This is a two part question, but first some background information: I have a
This is a two part question, but I want to make sure the first
This is kinda....a two part question. The first one is much more important than
This is a two part question - first I need to get every element
This is a two part question: Part 1 First, dealing with calculating the entropy
This is a two part question. The first part is converting the function below
This is a two part question from a WPF animation newbie. First, here is
This is a two part question. The first is a specific question about DataSets
This is actually a two part question. First,does the HttpContext.Current correspond to the current

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.