Jean-Paul Calderone wrote:
So I'm at a loss as to how to add an "idle" function that will allow my bot to periodically see whether it has something new to say to the user.
While this sometimes makes sense, it's usually *not* the approach you want to take. You're describing a solution which is essentially polling. And polling is not as good as responding to events. You *could* run a function ten times a second that looks around for a message to send and sends it if it finds one. Or, whatever event occurs which creates those messages could just send the message instead of putting it in a pile and waiting for your poller to find it.
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. So I really do think I need a way to call a function once every few seconds. 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. Then my "idler thread" could call the idle function, sleep for a few seconds, and repeat. But if there's a simple way to do this within Twisted, I'd like to learn what that is .
Now, from trawling the docs, I guess that I want to call reactor.callWhenRunning...
Probably not. If you want to run a function later (and that's how polling is generally implemented), you want reactor.callLater. If you want to run a function repeatedly at a fixed interval, twisted.internet.task.LoopingCall will help.
Thanks, I'll look at these.
You'll probably encounter the same problem with reactor.callLater as you were encountering with reactor.callWhenRunning, though. How do you get a reference to the bound method to pass in? That's simple - don't call the reactor method until you *have* an instance. You can call almost any reactor method even *after* the reactor is started; you do not have to set everything up before you call reactor.run.
OK, so somewhere within my B class, maybe in initDone, set it up there? That makes sense. Thanks, - Joe