6

Sorry for the vague title, but I couldn't figure out something better.

Are there any ways to get the current status of the reactor in twisted? By status I mean:

  • Listeners on ports
  • Connected protocols
  • Deferreds that are waiting to be fired
  • LoopingCalls that are running
  • Threads that are "idle"
  • and other "active" stuff...

Basically I'm trying to figure out if the reactor is just sitting idly. The question itself is broader than what I actually need to do, for the sake of completeness for stackoverflow.

Now, my current situation is as follows:

I'm using someone else's script that listens on many "dynamic" ports that are selected by this person's script, so I can't just use my script to do something like listenTCP, or TCP4ServerEndpoint and check the status of it. This is somewhat similar to non-PASV FTP where each listening port is discarded after each use. The other problem is that my own little program is also running in the same process, which I use it as an argument to start his program. It's basically an interface to his program. When all his things are done AND my things are done as well, I want to shutdown the reactor. Thus I'm trying to figure out when I can stop the reactor. My exact condition to shutdown is as follow:

  • Only listening on 1 port, and no other.
  • No deffereds nor loopingCalls that are gonna fire.

I searched but only found things like "if reactor.running" or Stop twisted reactor on a condition, which needs to keep track by using flags. I would rather not touch his code. If it's something that could contribute to twisted, I'd much rather do that. But if there is already an alternative, I'd rather not re-invent the wheel.

Does his script need to be changed so it notifies my script of these conditions?

Community
  • 1
  • 1
SleepyMan
  • 63
  • 4

1 Answers1

3

The code that you're calling into should be refactored to return a Deferred from something; probably an implementation of IService.stopService that stops it. The reason to do it this way is that you don't always know what will be sitting around in the reactor even when things are apparently "idle". For example, did you know that Twisted maintains a file descriptor (the "waker") for communication from threads? That the HTTP server maintains a callLater outstanding at all times to write the next access log entry? Other bits of infrastructure in third-party libraries might leave arbitrary different state in the reactor that is really none of your business.

The reactor does have a little introspection support though. For example, there is IReactorTime.getDelayedCalls, IReactorFDSet.getReaders/getWriters, and IReactorThreads.getThreadPool. However, there's no IReactorWin32Events.getAllEvents, so this isn't a wholly portable set of things. We'd definitely appreciate it if you wanted to contribute more reactor introspection implementations, for debugging and diagnostic purposes.

Glyph
  • 31,152
  • 11
  • 87
  • 129
  • Thanks @Glyph for the answer. Pressed Enter by habit. I really did not want to mess with his code coz my philosophy conflicts with his. But how would I integrate my code with his with IService? Different processes communicating through sockets? Well, either way, I'll notify him that his script needs change. For the mean time, I'll try learning a bit more about twisted's internals before actually contributing (I think it's good to learn about twisted's philosophy, right?). Is the only way to learn these things is to read the source or is it documented somewhere? – SleepyMan Aug 03 '12 at 14:24