New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
asyncore stores unnecessary object references #55466
Comments
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. |
Can you write a patch? |
--- asyncore.py 2010-09-15 22:18:21.000000000 +0600
+++ asyncore.py 2011-02-21 09:43:15.033839614 +0500
@@ -58,7 +58,7 @@
try:
socket_map
except NameError:
- socket_map = {}
+ socket_map = weakref.WeakValueDictionary()
def _strerror(err):
try: |
sorry, forgot |
I'd be fine with this. My only concern are performances. Results: plain dict: 722.26 Mb/sec |
Well, isn’t WeakRefDictionary faster than a dict here? |
WeakValue* |
Éric, the weak dicts are implemented in pure Python while built-in dicts are in C. That can make quite a difference. |
Oh, I guess I didn’t understand at all what the numbers meant. I shouldn’t have compared the values and assumed that less was better, my bad. |
Unless there's a way to automatically call close() when a dispatcher instance is no longer referenced (and I can't think of anything to do that) I'd say we better close this as rejected. |
Closing out as per msg128969. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: