Twisted IOCP Support
twisted-iocpsupport provides a C extension for Twisted's I/O Completion Ports (IOCP) reactor, primarily used on Windows for high-performance asynchronous I/O. It enhances the `twisted.internet.iocpreactor` module, making the IOCP reactor available for use. The current version is 25.10.1, with releases typically tied to Twisted's release cycle and Python version support, focusing on platform compatibility and binary wheels.
Common errors
-
ImportError: cannot import name '_iocpsupport' from 'twisted.internet.iocpreactor'
cause The `twisted-iocpsupport` C extension failed to install correctly, or you are attempting to use the IOCP reactor on a non-Windows platform.fixEnsure you are on a Windows operating system. Check the `pip install twisted-iocpsupport` logs for any build errors. If not on Windows, remove `iocpreactor.install()` or ensure its call is conditional on `os.name == 'nt'`. -
RuntimeError: The reactor is already installed.
cause The `iocpreactor.install()` function (or any `reactor.install()` variant) was called more than once in the application's lifecycle.fixCall `iocpreactor.install()` only once, ideally as the first Twisted-related operation in your application, before any `reactor` imports that might implicitly install a default reactor. -
error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/
cause During `pip install`, `twisted-iocpsupport` attempted to build from source on Windows, but the necessary C++ compiler tools were not found.fixInstall the 'Desktop development with C++' workload from Visual Studio Installer, or specifically the Microsoft C++ Build Tools, as indicated in the error message. Alternatively, try to use a Python version for which a pre-built wheel is available on PyPI.
Warnings
- gotcha This library is platform-specific and primarily designed for Windows. While it might install on other systems, its functionality (the IOCP reactor) is only available and beneficial on Windows.
- gotcha To activate the IOCP reactor provided by this extension, `iocpreactor.install()` must be explicitly called before other reactor-related operations.
- gotcha Installation can fail if pre-built wheels are not available for your specific Python version and architecture, requiring compilation from source.
Install
-
pip install twisted-iocpsupport
Imports
- iocpreactor
from twisted.internet import iocpreactor
Quickstart
import os
from twisted.internet import protocol, reactor
# This library provides the underlying C extension for the IOCP reactor.
# The IOCP reactor is Windows-specific and offers performance benefits there.
if os.name == 'nt':
from twisted.internet import iocpreactor
iocpreactor.install()
class Echo(protocol.Protocol):
def dataReceived(self, data):
self.transport.write(data)
class EchoFactory(protocol.Factory):
def buildProtocol(self, addr):
return Echo()
print("Starting IOCP reactor echo server on port 8000 (Windows only)...")
reactor.listenTCP(8000, EchoFactory())
reactor.run()
else:
print("IOCP reactor is Windows-specific. Skipping quickstart on non-Windows OS.")
print("Install and run on Windows to test this library's functionality.")