[Twisted-Python] how to get an idle callback while running a reactor?

Andrew Francis andrewfr_ice at yahoo.com
Sat Dec 20 17:13:01 EST 2008

Message: 6
Date: Sat, 20 Dec 2008 08:28:20 -0700
From: Joe Strout <joe at strout.net>
Subject: Re: [Twisted-Python] how to get an idle callback while
    running a    reactor?
To: Twisted general discussion <twisted-python at twistedmatrix.com>
Message-ID: <494D0F14.3000105 at strout.net>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Hi Joe:

>Well, the thing is that there will actually be a great many things that
>could cause it to need to send a message, such as the time, a change in
>the outside temperature, a certain change in the value of a stock fund,
>etc.  And the AIM interface isn't the only interface to this bot. 

I would argue all these things can be viewed external events - even time (imagine a clock source is calling your system every N time units). In turn, your programme reacts to the external events.  

At a more concrete level, you have to ask yourself questions like "is my programme a client, a server (or both)? That is, is my programme making connections? Or is it waiting for connections? This will affect how you structure and start-up your application. From I see SkippyTalkBot is a client.

>Now, that does suggest an alternative approach, which is to ignore
>twisted's event/idle mechanisms and use something external to it.  But
>I'm also fairly newish to Python, so I'm not sure exactly how to do that
>either -- reactor.run() is a blocking call, so I supposed I'd have to
>use threads.

Yes under the hood reactor.run() blocks. However the reactor is what effectively starts a Twisted application. I would argue that under most circumstances, you probably don't care that the reactor blocks because your application isn't doing anything until it receives an event from the reactor. As for threads. Off hand, I suspect you don't need threads .....

>But if there's a simple way to do this within Twisted, I'd like to learn
>what that is

Let us pretend your programme polls various network sources at regular intervals. So perhaps your code could look like:

if __name__ == "__main__":
   # it may be helpful to keep track of the task objects
   source['source-a] = task.LoopingCall(callback-for-stockTicker, ...)
   source['source-b] = task.loopingCall(callback-for-thermostat, ...)
   source['source-c] = task.loopingCall(callback-for-timer, ....)

   for source in sources:
       source.start(someInterval, True | False)

and each callback-for-X sets up the appropriate call to the remove source and its callback chain.

# assume we are using SOAP
def callback-for-stockTicker():
    ticker = soap.Proxy(....)



More information about the Twisted-Python mailing list