[issue19172] selectors: add keys() method

Charles-François Natali report at bugs.python.org
Sat Oct 5 16:40:38 CEST 2013


New submission from Charles-François Natali:

This adds a keys() method to selectors, to return all the registered keys.
It's useful, because one often needs to loop until all registered file objects have been unregistered e.g. (inspired from subprocess, see #18923):

while selector.keys():
   for key, events in selector.select():
       # process events

also, it can be useful if you want e.g. the list of all currently logged users, etc. It avoids having to maintain a separate data-structure tracking registered file objects.

The patch attached returns a new set upon each call: another way to handle this would be to just return the dictionary's .values() view: it would be faster and use less memory, but it's immutable and also this would prevent the user from doing:
for key in selectior.keys():
   if somecondition:
       selector.unregister(key.fileobj)

(since you'll get "dictonary changed size during iteration").

----------
components: Library (Lib)
files: selectors_keys.diff
keywords: needs review, patch
messages: 198988
nosy: haypo, neologix, pitrou
priority: normal
severity: normal
stage: patch review
status: open
title: selectors: add keys() method
type: enhancement
versions: Python 3.4
Added file: http://bugs.python.org/file31965/selectors_keys.diff

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue19172>
_______________________________________


More information about the Python-bugs-list mailing list