Message128909
asyncore.py:
-----------
def add_channel(self, map=None):
#self.log_info('adding channel %s' % self)
if map is None:
map = self._map
map[self._fileno] = self
---------
As we see, it add itself to a map, creating unnecessary refences to 'self'.
Such code should be rewritten via weakref. Now it's unknown when object will garbage collected. For example, if someone forget to call close(), object will stuck, eating file descriptor and memory...
When using weakref, we may guarantee (via callback fcuntion), that we call close() when last reference to object has been lost. Also, such behaviour guarantee, that object will be garbage collected when last user's reference has gone.
To approve my thoughts, see code:
------------------------------
class MyServer(asyncore.dispatcher):
def __init__(self, listenaddr):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.set_reuse_addr()
self.bind(listenaddr)
self.listen(5)
def handle_accept(self):
while 1:
r=self.accept()
if r is None:
break
my_conn_handler(r[0])
------------
As we see, we create a new instance via my_conn_handler(), and we do not store reference to it, but object will not be garbage collected. It's unexpected behaviour. |
|
Date |
User |
Action |
Args |
2011-02-20 15:44:07 | socketpair | set | recipients:
+ socketpair |
2011-02-20 15:44:06 | socketpair | set | messageid: <1298216646.94.0.460064768402.issue11257@psf.upfronthosting.co.za> |
2011-02-20 15:44:05 | socketpair | link | issue11257 messages |
2011-02-20 15:44:05 | socketpair | create | |
|