Mercurial > cosocket
changeset 20:dc3dce13dd3b
Fixed memory leak in channels.py, I think.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Sun, 19 Apr 2009 22:11:46 -0700 |
parents | 264e243e396c |
children | b77e679ce993 |
files | channels.py |
diffstat | 1 files changed, 9 insertions(+), 8 deletions(-) [+] |
line wrap: on
line diff
--- a/channels.py Sun Apr 19 21:46:09 2009 -0700 +++ b/channels.py Sun Apr 19 22:11:46 2009 -0700 @@ -1,6 +1,4 @@ -# TODO: Think about memory use here; we may want to use weakrefs or -# register to be notified of a dispatcher's abnormal death to get -# rid of their entry in the channel list. +import weakref _channels = {} @@ -11,9 +9,11 @@ def execute(self, dispatcher): if self.channel_name in _channels: - for receiver in _channels[self.channel_name]: - receiver.return_from_yield(self.message) - _channels[self.channel_name] = [] + for weakling in _channels[self.channel_name].itervaluerefs(): + receiver = weakling() + if receiver: + receiver.return_from_yield(self.message) + del _channels[self.channel_name] dispatcher.return_from_yield() class until_message_received(object): @@ -22,5 +22,6 @@ def execute(self, dispatcher): if self.channel_name not in _channels: - _channels[self.channel_name] = [] - _channels[self.channel_name].append(dispatcher) + _channels[self.channel_name] = weakref.WeakValueDictionary() + fd = dispatcher.socket.fileno() + _channels[self.channel_name][fd] = dispatcher