I recently started using twisted a couple of weeks ago. For those who don’t know, twisted is “event-driven networking engine written in Python”. The learning curve is pretty steep if you’ve never done asynchronous programming before. During the project I was working on, I ran into a condition where I thought I needed to restart the twisted reactor. According to everything I found online, restarting the reactor is not supported. But I can be stubborn so I tried to find a way anyway.
Restarting a Twisted Reactor
Let’s start by creating a pretty standard twisted server. We’ll subclass LineReceiver which basically makes a TCP server that accepts full lines of text, although it can also do raw data too. Let’s take a look at the code: