Search code examples
pythonpython-2.7twistedreactortwisted.internet

How to know reactor is running or not in python?


How to know reactor status (running or not) ?

I tried this(searched from google):

from twisted.internet import reactor
if reactor.callWhenRunning(lambda: None) is not None: 
    # do some work

It worked, but this seems like weird way.

What can be other ways of doing this ?


Solution

  • You do not state which reactor you are using, but this page says ReactorBase is the base class for Reactors.

    Also on the same page, it mentions an instance variable running that is further explained here.

    It says

    running = A bool which is True from during startup to during shutdown and False the rest of the time.

    With that information we can change your code to:

    if reactor.running:
        # do some work