module documentation

Threaded select reactor

The threadedselectreactor is a specialized reactor for integrating with arbitrary foreign event loop, such as those you find in GUI toolkits.

There are three things you'll need to do to use this reactor.

Install the reactor at the beginning of your program, before importing the rest of Twisted:

    | from twisted.internet import _threadedselect
    | _threadedselect.install()

Interleave this reactor with your foreign event loop, at some point after your event loop is initialized:

    | from twisted.internet import reactor
    | reactor.interleave(foreignEventLoopWakerFunction)
    | self.addSystemEventTrigger('after', 'shutdown', foreignEventLoopStop)

Instead of shutting down the foreign event loop directly, shut down the reactor:

    | from twisted.internet import reactor
    | reactor.stop()

In order for Twisted to do its work in the main thread (the thread that interleave is called from), a waker function is necessary. The waker function will be called from a "background" thread with one argument: func. The waker function's purpose is to call func() from the main thread. Many GUI toolkits ship with appropriate waker functions. One example of this is wxPython's wx.callAfter (may be wxCallAfter in older versions of wxPython). These would be used in place of "foreignEventLoopWakerFunction" in the above example.

The other integration point at which the foreign event loop and this reactor must integrate is shutdown. In order to ensure clean shutdown of Twisted, you must allow for Twisted to come to a complete stop before quitting the application. Typically, you will do this by setting up an after shutdown trigger to stop your foreign event loop, and call reactor.stop() where you would normally have initiated the shutdown procedure for the foreign event loop. Shutdown functions that could be used in place of "foreignEventloopStop" would be the ExitMainLoop method of the wxApp instance with wxPython.

Class ThreadedSelectReactor A threaded select() based reactor - runs on all POSIX platforms and on Win32.
Function install Configure the twisted mainloop to be run using the select() reactor.
Function raiseException Undocumented
Function _threadsafeSelect Invoke select. This will be called in a non-main thread, so it is very careful to work only on integers and avoid calling any application code.
Variable _log Undocumented
def install(): (source)

Configure the twisted mainloop to be run using the select() reactor.

def raiseException(e): (source)

Undocumented

def _threadsafeSelect(timeout: float|None, readmap: dict[int, IReadDescriptor], writemap: dict[int, IWriteDescriptor], handleResult: Callable[[list[int], list[int], dict[int, IReadDescriptor], dict[int, IWriteDescriptor], bool], None]): (source)

Invoke select. This will be called in a non-main thread, so it is very careful to work only on integers and avoid calling any application code.

Undocumented