tags:

views:

663

answers:

3

new to python and reading someone else's code:

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

+1  A: 

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.

Peter
This is true of some implementations of Python, but the Python language does not guarantee that the closing will happen as soon as the object goes out of scope. cf. jython
gnibbler
+6  A: 

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.

Alex Martelli
+1  A: 

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 __del__ 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.

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.

Mark Rushakoff