Reactor

Twisted implements the reactor design pattern, which describes how to obtain and redirect events from multiple sources to their respective handlers in a single thread.

The Twisted core is the reactor event loop. The event loop waits for these events and then processes them, abstracting specific behavior of a platform and presenting interfaces to facilitate the response.

The reactor is the main Twisted loop, and is responsible for calling the events at the appropriate time and alternating between the different connections to achieve (rather than simulate) concurrency.

For creating a reactor that's listening in a specific port, we can use the listenTCP() method. We will pass in the port and the Factory class that was created in the Factory section as parameters:

reactor.listenTCP(8080, MessageFactory())
reactor.run()

In this case, it is used to listen to TCP connections through port 8080. As a second parameter, an instance of our factory is passed, which, as we indicated previously, is responsible for assigning a protocol to each incoming connection.

Finally, we execute the main loop by calling the reactor.run() function.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset