Package: selectors2
Selectors2Backported, durable, and portable selectors designed to replace
the standard library selectors module.FeaturesSupport for all major platforms. (Linux, Mac OS, Windows)Support for Python 2.6 or later andJython.Support many different selectorsselect.kqueue(BSD, Mac OS)select.devpoll(Solaris)select.epoll(Linux 2.5.44+)select.poll(Linux, Mac OS)select.select- (Linux, Mac OS, Windows)Support forPEP 475(Retries system calls on interrupt)Support for modules which monkey-patch the standard library after import (like greenlet, gevent)Support for systems which define a selector being available but don’t actually implement it. ()AboutThis module was originally written by me for theurllib3project
(history in PR#1001) but it was decided that it would
be beneficial for everyone to have access to this work.All the additional features thatselectors2provides are real-world problems that have occurred
and been reported during the lifetime of its maintenance and use withinurllib3.If this work is useful to you,feel free to say thanks,
takes only a little time and really brightens my day! :cake:Can this module be used in place ofselectors?Yes! This module is a 1-to-1 drop-in replacement forselectorsand
provides all selector types that would be available inselectorsincludingDevpollSelector,KqueueSelector,EpollSelector,PollSelector, andSelectSelector.What is different betweenselectors2andselectors34?This module is similar toselectors34in that it supports Python 2.6 - 3.3
but differs in that this module also implements PEP 475 for the backported selectors.
This allows similar behaviour between Python 3.5+ selectors and selectors from before PEP 475.
Inselectors34, an interrupted system call would result in an incorrect return of no events, which
for some use cases is not an acceptable behavior.I will also add here thatselectors2also makes large improvements on the test suite surrounding it
providing 100% test coverage for each selector.  The test suite is also more robust and tests durability
of the selectors in many different situations that aren’t tested inselectors34.What types of objects are supported?At this current timeselectors2only support theSelectSelectorfor Windows which cannot select on non-socket objects.
On Linux and Mac OS, both sockets and pipes are supported (some other types may be supported as well, such as fifos or special file devices).What if I have to support a platform withoutselect.select?There are a few platforms that don’t have a selector available, notably
Google AppEngine. When running on those platforms any call toDefaultSelector()will raise aRuntimeErrorexplaining that there are no selectors available.LicenseThis module is dual-licensed under MIT and PSF License.Installation$ python-mpip install selectors2Usageimportsysimportselectors2asselectors# Use DefaultSelector, it picks the best# selector available for your platform! :)s=selectors.DefaultSelector()importsocket# We're going to use Google as an example.sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)sock.connect(("www.google.com",80))# Register the file to be watched for write availibility.s.register(sock,selectors.EVENT_WRITE)# Give a timeout in seconds or no# timeout to block until an event happens.events=s.select(timeout=1.0)# Loop over all events that happened.forkey,eventinevents:ifevent&selectors.EVENT_WRITE:key.fileobj.send(b'HEAD / HTTP/1.1\r\n\r\n')# Change what event you're waiting for.s.modify(sock,selectors.EVENT_READ)# Timeout of None let's the selector wait as long as it needs to.events=s.select(timeout=None)forkey,eventinevents:ifevent&selectors.EVENT_READ:data=key.fileobj.recv(4096)print(data)# Stop watching the socket.s.unregister(sock)sock.close()ChangelogRelease 2.0.2 (July 21, 2020)[BUGFIX] Added support forlongintegers in Python 2.x.Release 2.0.1 (August 17, 2017)[BUGFIX] Timeouts would not be properly recalculated after receiving an EINTR error.Release 2.0.0 (May 30, 2017)[FEATURE] Add support for Jython withJythonSelectSelector.[FEATURE] Add support for/dev/devpollwithDevpollSelector.[CHANGE] Raises aRuntimeErrorinstead ofValueErrorif there is no selector available.[CHANGE] No longer wraps exceptions inSelectorError, raises original exception including
in timeout situations.[BUGFIX] Detect defects in a system that defines a selector but does not implement it.[BUGFIX] Can now detect a change in theselectmodule after import such as whengevent.monkey.monkey_patch()is called before importingselectors2.Release 1.1.1 (February 6, 2017)[BUGFIX] Platforms that defineselect.kqueuewould not haveKqueueSelectoras theDefaultSelector.Release 1.1.0 (January 17, 2017)[FEATURE] Make system calls faster for Python versions that support PEP 475.[FEATURE] Wheels are now universal.Release 1.0.0 (November 3, 2016)Initial implementation ofselectors2.