should I call close() after urllib.urlopen()?

PythonUrllib

Python Problem Overview


I'm new to Python and reading someone else's code:

should urllib.urlopen() be followed by urllib.close()? Otherwise, one would leak connections, correct?

Python Solutions


Solution 1 - Python

The close method must be called on the result of urllib.urlopen, not on the urllib module itself as you're thinking about (as you mention urllib.close -- which doesn't exist).

The best approach: instead of x = urllib.urlopen(u) etc, use:

import contextlib

with contextlib.closing(urllib.urlopen(u)) as x:
   ...use x at will here...

The with statement, and the closing context manager, will ensure proper closure even in presence of exceptions.

Solution 2 - Python

Like @Peter says, out-of-scope opened URLs will become eligible for garbage collection.

However, also note that urllib.py defines:

 def __del__(self):
        self.close()

This means that when the reference count for that instance reaches zero, its http://docs.python.org/reference/datamodel.html#object.__del__">`__del__`</a> method will be called, and thus its close method will be called as well. The most "normal" way for the reference count to reach zero is to simply let the instance go out of scope, but there's nothing strictly stopping you from an explicit del x early (however it doesn’t directly call __del__ but just decrements the reference count by one).

It's certainly good style to explicitly close your resources -- especially when your application runs the risk of using too much of said resources -- but Python will automatically clean up for you if you don't do anything funny like maintaining (circular?) references to instances that you don't need any more.

Solution 3 - Python

Strictly speaking, this is true. But in practice, once (if) urllib goes out of scope, the connection will be closed by the automatic garbage collector.

Solution 4 - Python

You basically do need to explicitly close your connection when using IronPython. The automatic closing on going out of scope relies on the garbage collection. I ran into a situation where the garbage collection did not run for so long that Windows ran out of sockets. I was polling a webserver at high frequency (i.e. as high as IronPython and the connection would allow, ~7Hz). I could see the "established connections" (i.e. sockets in use) go up and up on PerfMon. The solution was to call gc.collect() after every call to urlopen.

Solution 5 - Python

> urllib.request module uses HTTP/1.1 and includes Connection:close header in its HTTP requests.

It's from official docs, you can check it here.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNikitaView Question on Stackoverflow
Solution 1 - PythonAlex MartelliView Answer on Stackoverflow
Solution 2 - PythonMark RushakoffView Answer on Stackoverflow
Solution 3 - PythonPeterView Answer on Stackoverflow
Solution 4 - PythonJann PoppingaView Answer on Stackoverflow
Solution 5 - PythonAndré CarvalhoView Answer on Stackoverflow